Gets a function for these inputs, defining it if necessary. `args` and `kwargs` can be None if this `Function` was created with an `input_signature`. Args: args: The varargs for the Python function. kwargs: The keyword args for the Python function. Returns: A gra
(self, args, kwargs)
| 2080 | return graph_function, args, kwargs |
| 2081 | |
| 2082 | def _maybe_define_function(self, args, kwargs): |
| 2083 | """Gets a function for these inputs, defining it if necessary. |
| 2084 | |
| 2085 | `args` and `kwargs` can be None if this `Function` was created with an |
| 2086 | `input_signature`. |
| 2087 | |
| 2088 | Args: |
| 2089 | args: The varargs for the Python function. |
| 2090 | kwargs: The keyword args for the Python function. |
| 2091 | |
| 2092 | Returns: |
| 2093 | A graph function corresponding to the input signature implied by args and |
| 2094 | kwargs, as well as the inputs that the object should be called with. |
| 2095 | |
| 2096 | Raises: |
| 2097 | ValueError: If inputs are incompatible with the input signature. |
| 2098 | TypeError: If the function inputs include non-hashable objects |
| 2099 | RuntimeError: If there's an internal bug (inconsistency) in handling |
| 2100 | shape relaxation retracing. |
| 2101 | """ |
| 2102 | if self.input_signature is None or args is not None or kwargs is not None: |
| 2103 | args, kwargs = self._function_spec.canonicalize_function_inputs( |
| 2104 | *args, **kwargs) |
| 2105 | |
| 2106 | cache_key = self._cache_key(args, kwargs) |
| 2107 | |
| 2108 | try: |
| 2109 | hash(cache_key) |
| 2110 | except TypeError as e: |
| 2111 | raise TypeError( |
| 2112 | "Arguments supplied to `defun`-generated functions must be" |
| 2113 | " hashable. Original error: %s" % e) |
| 2114 | |
| 2115 | with self._lock: |
| 2116 | graph_function = self._function_cache.primary.get(cache_key, None) |
| 2117 | if graph_function is not None: |
| 2118 | return graph_function, args, kwargs |
| 2119 | |
| 2120 | logging.vlog(1, |
| 2121 | "Creating new FuncGraph for Python function %r (key: %r)", |
| 2122 | self._python_function, cache_key) |
| 2123 | logging.vlog(2, |
| 2124 | "Python function signature [args: %s] [kwargs: %s]", |
| 2125 | args, |
| 2126 | kwargs) |
| 2127 | |
| 2128 | call_context_key = cache_key.replace(input_signature=None) |
| 2129 | |
| 2130 | ag_status = ( |
| 2131 | ag_ctx.Status.ENABLED if self._autograph else ag_ctx.Status.DISABLED) |
| 2132 | with ag_ctx.ControlStatusCtx( |
| 2133 | status=ag_status, options=self._autograph_options): |
| 2134 | |
| 2135 | # Build a function with shape relaxation retracing if: |
| 2136 | # 1. shape relaxation is explicitly enabled |
| 2137 | # and 2. there's no provided input signature |
| 2138 | # and 3. there's been a cache miss for this calling context |
| 2139 | if (self._experimental_relax_shapes |
no test coverage detected