GTIR domain inference #1568
This function looks unnecessarily complicated, just a dict with start end stop of the ranges should be sufficient. This would be a good doctest to add.
>>> cartesian_domain({"IDim": (0, 10), "JDim": (0, 20)})
c⟨ IDim: [0, 10), JDim: [0, 20) ⟩
You only need the constant folding for readability in your tests so this should happen there. The makers should not contain any calls to passes or "optimizations", but just be simple tools that ease creation of nodes.
You will need the offset provider for connectivities (i.e. unstructured) when you extend this to not only support cartesian dimensions later on. It will be an argument to the final pass that should be forwarded here.
current_dim = offset_provider[off]
assert isinstance(current_dim, Dimension)
For now it would make sense to
``
offset_provider={"IOff": Dimension(...), "JOff": Dimension(...)}
Pass it as an argument to infer_as_fieldop` and pass it down the chain until here.I think what you call stencil here is an applied_fieldop, e.g as_fieldop(stencil, domain)(field_args...). A stencil is a function from Iterators to a value, e.g. lambda x: deref(x).
just for readability (stencil has a different meaning here see comment above).
# `as_fieldop(stencil, domain)(inputs...)`
stencil, inputs = applied_fieldop.fun.args[0], applied_fieldop.args
# TODO: until TraceShifts directly supporty stencils we just wrap our expression into
# a dummy closure in this helper function.
def trace_shifts(stencil: ir.Expr, inputs: list[ir.Expr], domain: ir.Expr):
node = itir.StencilClosure(
stencil=stencil,
inputs=inputs_node,
output=im.ref(out_field_name),
domain=SymbolicDomain.as_expr(symbolic_domain),
)
return TraceShifts.apply(node)
You can parameterize this test (https://docs.pytest.org/en/7.0.x/how-to/parametrize.html) and which should make it a little simpler because you don't need tmp, domain, expected to be arrays.
First course round.
Having multiple variables containing the inputs doesn't feel right, maybe you can get rid of it.
>>> str(cartesian_domain({"IDim": (0, 10), "JDim": (0, 20)}))
The makers are really only plain simple helper functions. Dependency-wise it should rather be the other way around: Use the cartesian_domain in SymbolicDomain. It might make sense to allow using either strings or Dimensions as the key for the dict here (since in SymbolicDomain you might have Dimensions at hand).
The user should be free to use any axis names he likes.
Should be possible to remove the function and also absorb it into translate domain.
Note to myself: One function for itir.Program with temporaries, one without (only the simple for loop). When writing temporary pass introduce ProgramWithUnresolvedTemporaries that allows _gtmp_auto_domain and potentially move the inference (continuing to build upon the primitive function operaton on as_fieldop here) there.
I noticed that this is too restrictive for what we need. That was not clear when this project started. We now allow also pure field expressions at this level, e.g.
__field_operator_testee(
__sym_1,
out,
____sym_1_size_0,
____sym_1_size_1,
____sym_1_size_2,
__out_size_0,
__out_size_1,
__out_size_2
) {
testee = λ(a) → a;
out @ c⟨ IDimₕ: [0, __out_size_0), JDimₕ: [0, __out_size_1), KDimᵥ: [0, __out_size_2) ⟩
← testee(__sym_1);
}
maybe
target_domain: SymbolicDomain,
if isinstance(applied_fieldop, itir.SymRef):
accessed_domains = {applied_fieldop.id: input_domain}
return applied_fieldop, accessed_domains
assert isinstance(applied_fieldop, itir.FunCall) and isinstance(
This should work for your example @havogt here https://github.com/GridTools/gt4py/pull/1568/files#r1672562626. Tuples and lets are not yet supported by this pass. The latter can be solved by inlining first.
def domain(grid_type: common.GridType, ranges: Dict[Union[common.Dimension, str], Tuple[Any, Any]]) -> itir.FunCall:
I would propose this signature.
im.domain(self.grid_type, ...)
Is this change required? I hope not then we shouldn't do it here to avoid creating an unnecessary dependency between the passes. If a cleaner IR is required for tests / debugging this should be done outside of the pass.
input_domain: SymbolicDomain | ir.FunCall,
and then below you can do the canonicalization
if isinstance(input_domain, ir.FunCall):
input_domain = SymbolicDomain.from_expr(input_domain)
The
_get_symbolic_domain is then not needed anymore.Let's move this outside of the function to keep the reader focused.
Let's remove this class (at least for now) and transform all class methods & static functions into free functions. There appears to be no need to have a class.
Requested changes.
Yes, it is required to get rid of minimum/maximum in the SymbolicDomains returned by infer_as_fieldop. Should the ConstantFolding be done directly in the tests in the end or somewhere else?
As discussed, I removed the ConstantFolding from domain_union and do it now directly in the tests.
updating the domain should only be done if the original domain is AUTO_DOMAIN, correct? I am wondering if we should split this pass in 2. One that propagates domains between set_at (i.e. resolves AUTO_DOMAIN) and one that just annotates as_fieldops. I find it confusing that we do both in the same pass, but not sure if I am missing something.
ignore this, it was an old comment that I didn't submit.
ah maybe it's still valid.
Thanks, I changed it.
I am also not sure if I am missing something, but I think this is only possible with some code duplication.
One option could be to add a pass that infers the domains of all as_fieldops which is then only called by DaCe.
Let's discuss this next time we talk.
Approved these changes.
As discussed: We discovered that running the domain propagation prior to temporary extraction should make all temporary related code in the propagation unnecessary and can hence be removed. We've placed a todo (in the let branch) and kept it in here for now so that we can move on.
Implemented (in the let branch).
tehrengruber
SF-N
Uses TraceShifts to infer the minimal domain of (nested)
as_fieldops and SetAt:Domain inference of let-statements is not implemented yet and will be done in PR #1591
PR Description
infer_as_fieldopandinfer_programdomainandas_fieldopin ir.makersitir.Literals to simplify resulting domainsTodos
as_fieldops (cartesian_domain)Note
I agree that my contribution in this PR is additionally licensed under the BSD-3-Clause license (SPDX short identifier).