2022-09-22 12:26:48 -07:00
|
|
|
# Copyright 2018 The JAX Authors.
|
2018-11-17 18:03:33 -08:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2022-06-02 11:50:03 -07:00
|
|
|
from __future__ import annotations
|
2018-11-17 18:03:33 -08:00
|
|
|
|
2024-06-26 14:44:52 -04:00
|
|
|
from collections.abc import Callable
|
2023-02-17 14:03:28 -08:00
|
|
|
import types
|
2024-06-26 14:44:52 -04:00
|
|
|
from typing import Any, TypeVar
|
2018-11-17 18:03:33 -08:00
|
|
|
|
2022-12-16 20:59:41 -08:00
|
|
|
from jax._src import core
|
2023-03-30 06:12:10 -07:00
|
|
|
from jax._src import traceback_util
|
2024-12-12 09:49:06 -08:00
|
|
|
from jax._src.core import Primitive, valid_jaxtype, get_aval
|
2024-09-19 09:41:28 -07:00
|
|
|
from jax._src.tree_util import register_pytree_node, tree_map
|
2022-12-16 16:00:38 -08:00
|
|
|
from jax._src.typing import Array, ArrayLike
|
2023-03-30 06:12:10 -07:00
|
|
|
from jax._src.util import safe_map
|
2018-11-21 13:20:44 -08:00
|
|
|
|
2020-10-26 10:03:06 -07:00
|
|
|
traceback_util.register_exclusion(__file__)
|
|
|
|
|
2022-12-16 16:00:38 -08:00
|
|
|
T = TypeVar('T')
|
2020-03-18 17:06:05 -04:00
|
|
|
|
2018-11-21 13:20:44 -08:00
|
|
|
map = safe_map
|
2018-11-17 18:03:33 -08:00
|
|
|
|
2022-12-16 16:00:38 -08:00
|
|
|
def add_jaxvals(x: ArrayLike, y: ArrayLike) -> Array:
|
2025-04-15 11:01:49 -07:00
|
|
|
x, y = core.standard_insert_pvary(x, y)
|
2023-12-22 15:53:48 -08:00
|
|
|
return add_jaxvals_p.bind(x, y)
|
|
|
|
|
|
|
|
add_jaxvals_p = Primitive('add_any')
|
|
|
|
add_any_p = add_jaxvals_p
|
|
|
|
|
|
|
|
@add_jaxvals_p.def_impl
|
|
|
|
def add_impl(x, y):
|
|
|
|
return raw_jaxval_adders[type(x)](x, y)
|
|
|
|
raw_jaxval_adders = {} # type: ignore
|
|
|
|
|
|
|
|
@add_jaxvals_p.def_abstract_eval
|
|
|
|
def add_abstract(x, y):
|
2024-11-05 07:16:32 -08:00
|
|
|
assert core.typematch(x, y)
|
|
|
|
return x
|
2018-11-17 18:03:33 -08:00
|
|
|
|
2023-12-20 21:00:08 -08:00
|
|
|
def zeros_like_aval(aval: core.AbstractValue) -> Array:
|
|
|
|
return aval_zeros_likers[type(aval)](aval)
|
|
|
|
aval_zeros_likers: dict[type, Callable[[Any], Array]] = {}
|
2018-11-17 18:03:33 -08:00
|
|
|
|
2023-12-20 21:00:08 -08:00
|
|
|
def zeros_like_jaxval(val):
|
2024-12-12 09:49:06 -08:00
|
|
|
return zeros_like_aval(core.get_aval(val))
|
2018-11-17 18:03:33 -08:00
|
|
|
|
2023-07-21 14:20:39 -04:00
|
|
|
def instantiate(z: Zero | Array) -> Array:
|
2023-12-20 21:00:42 -08:00
|
|
|
if isinstance(z, Zero):
|
2022-06-02 11:50:03 -07:00
|
|
|
return zeros_like_aval(z.aval)
|
2023-12-20 21:00:42 -08:00
|
|
|
return z
|
2022-06-02 11:50:03 -07:00
|
|
|
|
2018-11-17 18:03:33 -08:00
|
|
|
|
2020-05-27 13:57:47 +00:00
|
|
|
class Zero:
|
|
|
|
__slots__ = ['aval']
|
2022-12-16 16:00:38 -08:00
|
|
|
def __init__(self, aval: core.AbstractValue):
|
2020-05-27 13:57:47 +00:00
|
|
|
self.aval = aval
|
2022-12-16 16:00:38 -08:00
|
|
|
def __repr__(self) -> str:
|
2022-05-12 19:13:00 +01:00
|
|
|
return f'Zero({self.aval})'
|
2020-05-27 13:57:47 +00:00
|
|
|
@staticmethod
|
2024-09-18 13:43:14 -07:00
|
|
|
def from_primal_value(val: Any) -> Zero:
|
2024-12-12 09:49:06 -08:00
|
|
|
return Zero(get_aval(val).to_tangent_aval())
|
2018-11-17 18:03:33 -08:00
|
|
|
|
2020-05-27 13:57:47 +00:00
|
|
|
register_pytree_node(Zero, lambda z: ((), z.aval), lambda aval, _: Zero(aval))
|
2020-04-23 13:12:24 -07:00
|
|
|
|
|
|
|
|
2022-12-16 16:00:38 -08:00
|
|
|
def _stop_gradient_impl(x: T) -> T:
|
2020-04-23 13:12:24 -07:00
|
|
|
if not valid_jaxtype(x):
|
|
|
|
raise TypeError("stop_gradient only works on valid JAX arrays, but "
|
|
|
|
f"input argument is: {x}")
|
|
|
|
return x
|
|
|
|
|
2021-03-02 09:42:12 -08:00
|
|
|
stop_gradient_p : Primitive = Primitive('stop_gradient')
|
2020-04-23 13:12:24 -07:00
|
|
|
stop_gradient_p.def_impl(_stop_gradient_impl)
|
|
|
|
stop_gradient_p.def_abstract_eval(lambda x: x)
|
2023-02-17 14:03:28 -08:00
|
|
|
|
|
|
|
|
2024-09-18 13:43:14 -07:00
|
|
|
# User-facing version of `Zero`
|
2023-02-17 14:03:28 -08:00
|
|
|
class SymbolicZero:
|
|
|
|
def __init__(self, aval: core.AbstractValue) -> None:
|
|
|
|
self.aval = aval
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return self.__class__.__name__
|
|
|
|
|
|
|
|
# TODO(mattjj,frostig): this forwards attr lookup to self.aval delegate;
|
|
|
|
# should dedup with core.Tracer.__getattr__ which does the same thing
|
|
|
|
def __getattr__(self, name):
|
|
|
|
# if the aval property raises an AttributeError, gets caught here
|
|
|
|
try:
|
|
|
|
attr = getattr(self.aval, name)
|
|
|
|
except KeyError as err:
|
|
|
|
raise AttributeError(
|
|
|
|
f"{self.__class__.__name__} has no attribute {name}"
|
|
|
|
) from err
|
|
|
|
else:
|
|
|
|
t = type(attr)
|
|
|
|
if t is core.aval_property:
|
|
|
|
return attr.fget(self)
|
|
|
|
elif t is core.aval_method:
|
|
|
|
return types.MethodType(attr.fun, self)
|
|
|
|
else:
|
|
|
|
return attr
|
|
|
|
|
2024-09-18 13:43:14 -07:00
|
|
|
@staticmethod
|
|
|
|
def from_primal_value(val: Any) -> SymbolicZero:
|
|
|
|
return SymbolicZero(get_aval(val).to_tangent_aval())
|
|
|
|
|
2024-09-19 09:41:28 -07:00
|
|
|
def zero_from_primal(val, symbolic_zeros=False):
|
|
|
|
def f(x):
|
|
|
|
tangent_aval = get_aval(x).to_tangent_aval()
|
|
|
|
if symbolic_zeros:
|
|
|
|
return SymbolicZero(tangent_aval)
|
|
|
|
else:
|
|
|
|
return zeros_like_aval(tangent_aval)
|
|
|
|
return tree_map(f, val)
|
|
|
|
|
2023-02-17 14:03:28 -08:00
|
|
|
JaxTypeOrTracer = Any
|
|
|
|
|
|
|
|
def replace_internal_symbolic_zeros(
|
2023-07-21 14:20:39 -04:00
|
|
|
x: JaxTypeOrTracer | Zero) -> JaxTypeOrTracer | SymbolicZero:
|
2023-02-17 14:03:28 -08:00
|
|
|
return SymbolicZero(x.aval) if type(x) is Zero else x
|
|
|
|
|
|
|
|
def replace_rule_output_symbolic_zeros(
|
2023-07-21 14:20:39 -04:00
|
|
|
x: JaxTypeOrTracer | SymbolicZero) -> JaxTypeOrTracer | Zero:
|
2023-02-17 14:03:28 -08:00
|
|
|
return Zero(x.aval) if type(x) is SymbolicZero else x
|
2023-12-20 21:00:08 -08:00
|
|
|
|
|
|
|
|
|
|
|
# TODO(mattjj): remove these after fixing downstream users relying on them
|
|
|
|
zeros_like_p: Primitive = Primitive('zeros_like')
|