From f914a19b7836498f4ccd81e01fdb7b1e3a57b5ba Mon Sep 17 00:00:00 2001 From: Joost van Zwieten Date: Mon, 26 Feb 2024 14:59:31 +0100 Subject: [PATCH 1/2] replace const Arrays with Constants in optimize To speed-up repeated evaluation of the same `Evaluable`, this patch introduces `Evaluable._deep_flatten_constants`, which replaces constant `Array`s with `Constant`s, excluding the no-op arrays `Transpose` and `InsertAxis`, and includes this in `optimized_for_numpy`. --- nutils/evaluable.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/nutils/evaluable.py b/nutils/evaluable.py index 0e9507849..9140a78da 100644 --- a/nutils/evaluable.py +++ b/nutils/evaluable.py @@ -449,6 +449,7 @@ def _simplified(self): @cached_property def optimized_for_numpy(self): retval = self.simplified._optimized_for_numpy1() or self + retval = retval._deep_flatten_constants() or retval return retval._combine_loop_concatenates(frozenset()) @replace(depthfirst=True, recursive=True) @@ -462,6 +463,11 @@ def _optimized_for_numpy1(obj): def _optimized_for_numpy(self): return + @replace(depthfirst=False, recursive=False) + def _deep_flatten_constants(self): + if isinstance(self, Array): + return self._flatten_constant() + @cached_property def _loop_concatenate_deps(self): deps = [] @@ -997,6 +1003,10 @@ def _const_uniform(self): lower, upper = self._intbounds return lower if lower == upper else None + def _flatten_constant(self): + if self.isconstant: + return constant(self.eval()) + class Orthonormal(Array): 'make a vector orthonormal to a subspace' @@ -1177,6 +1187,9 @@ def _const_uniform(self): if self.ndim == 0: return self.dtype(self.value[()]) + def _flatten_constant(self): + pass + class InsertAxis(Array): @@ -1296,6 +1309,9 @@ def _intbounds_impl(self): def _const_uniform(self): return self.func._const_uniform + def _flatten_constant(self): + pass + class Transpose(Array): @@ -1491,6 +1507,9 @@ def _intbounds_impl(self): def _const_uniform(self): return self.func._const_uniform + def _flatten_constant(self): + pass + class Product(Array): From 733bd997b660cfc521cf0734a975fa32f7ed1094 Mon Sep 17 00:00:00 2001 From: Joost van Zwieten Date: Mon, 26 Feb 2024 15:04:11 +0100 Subject: [PATCH 2/2] remove const Pointwise to Constant optimization This patch removes the special-cased optimization for transforming constant `Pointwise`s to `Constant`s in favour of the just introduced, generic `_deep_flatten_constants`. --- nutils/evaluable.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/nutils/evaluable.py b/nutils/evaluable.py index 9140a78da..208f75a74 100644 --- a/nutils/evaluable.py +++ b/nutils/evaluable.py @@ -2197,11 +2197,6 @@ def _simplified(self): if len(where) != self.ndim: return align(self._newargs(*uninserted), where, self.shape) - def _optimized_for_numpy(self): - if self.isconstant: - retval = self.eval() - return constant(retval) - def _derivative(self, var, seen): if self.dtype == complex or var.dtype == complex: raise NotImplementedError('The complex derivative is not implemented.')