inputs
stringlengths
312
52k
targets
stringlengths
1
3.1k
block_type
stringclasses
11 values
scenario
stringclasses
7 values
<filename>camp_zipnerf/internal/stepfun.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating step functions (piecewise-constant 1D functions). We have a shared naming and dimension convention for these functions. All input/output step functions are assumed to be aligned along the last axis. `t` always indicates the x coordinates of the *endpoints* of a step function. `y` indicates unconstrained values for the *bins* of a step function `w` indicates bin weights that sum to <= 1. `p` indicates non-negative bin values that *integrate* to <= 1. """ from internal import linspline from internal import math from internal import utils import jax import jax.numpy as jnp import numpy as np def query(tq, t, y, left=None, right=None): """Query step function (t, y) at locations tq. Edges repeat by default.""" utils.assert_valid_stepfun(t, y) # Query the step function to recover the interval value. (i0, i1), ((yq, _),) = math.sorted_lookup(tq, t, (y,), utils.device_is_tpu()) # Apply boundary conditions. left = y[Ellipsis, :1] if left is None else left right = y[Ellipsis, -1:] if right is None else right yq = math.select([(i1 == 0, left), (i0 == y.shape[-1], right)], yq) return yq def weight_to_pdf(t, w): """Turn a vector of weights that sums to 1 into a PDF that integrates to 1.""" utils.assert_valid_stepfun(t, w) td = jnp.diff(t) return jnp.where(td < np.finfo(np.float32).tiny, 0, math.safe_div(w, td)) def pdf_to_weight(t, p): """Turn a PDF that integrates to 1 into a vector of weights that sums to 1.""" utils.assert_valid_stepfun(t, p) return p * jnp.diff(t) def integrate_weights(w): """Compute<fim_suffix><fim_middle> the cumulative sum of w, assuming all weight vectors sum to 1. The output's size on the last dimension is one greater than that of the input, because we're computing the integral corresponding to the endpoints of a step function, not the integral of the interior/bin values. Args: w: Tensor, which will be integrated along the last axis. This is assumed to sum to 1 along the last axis, and this function will (silently) break if that is not the case. Returns: cw0: Tensor, the integral of w, where cw0[..., 0] = 0 and cw0[..., -1] = 1 """
the cumulative sum of w, assuming all weight vectors sum to 1. The output's size on the last dimension is one greater than that of the input, because we're computing the integral corresponding to the endpoints of a step function, not the integral of the interior/bin values. Args: w: Tensor, which will be integrated along the last axis. This is assumed to sum to 1 along the last axis, and this function will (silently) break if that is not the case. Returns: cw0: Tensor, the integral of w, where cw0[..., 0] = 0 and cw0[..., -1] = 1 """
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/render.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Helper functions for shooting and rendering rays.""" import jax import jax.numpy as jnp import jax.scipy as jsp from internal import math from internal import stepfun def lift_gaussian(d, t_mean, t_var, r_var, diag): """Lift a Gaussian defined along a ray to 3D coordinates.""" mean = d[Ellipsis, None, :] * t_mean[Ellipsis, None] d_mag_sq = jnp.maximum(1e-10, jnp.sum(d**2, axis=-1, keepdims=True)) if diag: d_outer_diag = d**2 null_outer_diag = 1 - d_outer_diag / d_mag_sq t_cov_diag = t_var[Ellipsis, None] * d_outer_diag[Ellipsis, None, :] xy_cov_diag = r_var[Ellipsis, None] * null_outer_diag[Ellipsis, None, :] cov_diag = t_cov_diag + xy_cov_diag return mean, cov_diag else: d_outer = d[Ellipsis, :, None] * d[Ellipsis, None, :] eye = jnp.eye(d.shape[-1]) null_outer = eye - d[Ellipsis, :, None] * (d / d_mag_sq)[Ellipsis, None, :] t_cov = t_var[Ellipsis, None, None] * d_outer[Ellipsis, None, :, :] xy_cov = r_var[Ellipsis, None, None] * null_outer[Ellipsis, None, :, :] cov = t_cov + xy_cov return mean, cov def gaussianize_frustum(t0, t1): """Convert<fim_suffix><fim_middle> intervals along a conical frustum into means and variances."""
intervals along a conical frustum into means and variances."""
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/math.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Mathy utility functions.""" import functools import jax import jax.numpy as jnp import numpy as np tiny_val = np.float32(np.finfo(np.float32).tiny) min_val = np.float32(np.finfo(np.float32).min) max_val = np.float32(np.finfo(np.float32).max) def laplace_cdf(x, beta): alpha = 1 / beta return alpha * (0.5 + 0.5 * safe_sign(x) * (jnp.exp(-jnp.abs(x) / beta) - 1)) def scaled_softplus(x, scale=100.0): return (1.0 / scale) * jax.nn.softplus(scale * x) def matmul(a, b): """jnp.matmul defaults to bfloat16, but this helper function doesn't.""" return jnp.matmul(a, b, precision=jax.lax.Precision.HIGHEST) def unstack(x, axis=0): return tuple( jnp.squeeze(z, axis=axis) for z in jnp.split(x, x.shape[axis], axis=axis) ) @jax.custom_jvp def plus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, tiny_val, jnp.nextafter(jnp.float32(x), jnp.inf) ) @jax.custom_jvp def minus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, -tiny_val, jnp.nextafter(jnp.float32(x), -jnp.inf) ) @plus_eps.defjvp def plus_eps_jvp(primals, tangents): """Make plus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return plus_eps(*primals), tangents[0] @minus_eps.defjvp def minus_eps_jvp(primals, tangents): """Make minus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return minus_eps(*primals), tangents[0] @jax.custom_jvp def expm1(x): """jnp.expm1() has inaccurate gradients when x << 0, this doesn't.""" return jnp.expm1(x) @expm1.defjvp def expm1_jvp(primals, tangents): return expm1(*primals), tangents[0] * jnp.exp(primals[0]) def safe_trig_helper(x, fn, t=100 * jnp.pi): """Helper function used by safe_cos/safe_sin: mods x before sin()/cos().""" return fn(jnp.nan_to_num(jnp.where(jnp.abs(x) < t, x, x % t))) def safe_cos(x): """jnp.cos() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.cos) def safe_sin(x): """jnp.sin() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.sin) @jax.custom_vjp def safe_arctan2(x1, x2): return safe_arctan2_fwd(x1, x2)[0] def safe_arctan2_fwd(x1, x2): return jnp.arctan2(x1, x2), (x1, x2) def safe_arctan2_bwd(res, g): x1, x2 = res denom = remove_zero(x1**2 + x2**2) d1 = g * (x2 / denom) d2 = g * (-x1 / denom) return d1, d2 safe_arctan2.defvjp(safe_arctan2_fwd, safe_arctan2_bwd) def generate_clip_nograd_fn(a_min, a_max): """Generates a function that clips to [a_min, a_max] with no grad effects.""" @jax.custom_jvp def clip_nograd(a): """Clamps `a` from above and below.""" return jnp.clip(a, a_min, a_max) @clip_nograd.defjvp def clip_nograd_jvp(primals, tangents): """Override clips()'s gradient to be a no-op.""" return clip_nograd(primals[0]), tangents[0] return clip_nograd clip_finite_nograd = generate_clip_nograd_fn(min_val, max_val) clip_pos_finite_nograd = generate_clip_nograd_fn(tiny_val, max_val) def clip_pos(x): """Clamps `x` from below to be positive.""" return jnp.maximum(tiny_val, x) def safe_sign(x): """jnp.sign(x) except x=0 is assumed to have a sign of +1, not 0.""" return jnp.where(x < 0, -1, +1) def remove_zero(x): """Shifts `x` away from 0.""" return jnp.where(jnp.abs(x) < tiny_val, tiny_val, x) def clip_finite(x): return jnp.clip(x, min_val, max_val) @jax.custom_vjp def safe_div(n, d): """Divide `n` by `d` but the value and gradient never nan out.""" return safe_div_fwd(n, d)[0] def safe_div_fwd(n, d): r = jnp.clip(n / remove_zero(d), min_val, max_val) return jnp.where(jnp.abs(d) < tiny_val, 0, r), (d, r) def safe_div_bwd(res, g): d, r = res dn = jnp.clip(g / remove_zero(d), min_val, max_val) dd = jnp.clip(-g * r / remove_zero(d), min_val, max_val) return dn, dd safe_div.defvjp(safe_div_fwd, safe_div_bwd) def generate_safe_fn(fn, grad_fn, x_range): """Generate's a `safe` fn() where inputs are clipped in fwd and bwd passes.""" @jax.custom_jvp def safe_fn(x): """fn()<fim_suffix><fim_middle> with clipped inputs."""
with clipped inputs."""
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/linspline.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Helper functions for linear splines.""" import functools from internal import math from internal import utils import jax from jax.experimental import checkify import jax.numpy as jnp def check_zero_endpoints(y): checkify.check(jnp.all(y[Ellipsis, 0] == 0), 'Splines must all start with 0.') checkify.check(jnp.all(y[Ellipsis, -1] == 0), 'Splines must all end with 0.') def query(tq, t, v): """Query linear spline (t, v) at tq.""" utils.assert_valid_linspline(t, v) interp = functools.partial(jnp.interp, left=0, right=0) return jnp.vectorize(interp, signature='(n),(m),(m)->(n)')(tq, t, v) def integrate(t, w): """Integrate (t, w) according to the trapezoid rule.""" utils.assert_valid_linspline(t, w) return 0.5 * jnp.sum((w[Ellipsis, :-1] + w[Ellipsis, 1:]) * jnp.diff(t), axis=-1) def normalize(t, w, eps=jnp.finfo(jnp.float32).eps ** 2): """Make w integrate to 1.""" utils.assert_valid_linspline(t, w) return w / jnp.maximum(eps, integrate(t, w))[Ellipsis, None] def insert_knot(ti, t, y): """Inserts knots ti into the linear spline (t, w). Assumes zero endpoints.""" utils.assert_valid_linspline(t, y) check_zero_endpoints(y) # Compute the spline value at the insertion points. yi = query(ti, t, y) # Concatenate the insertion points and values onto the end of each spline. ti_ex = jnp.broadcast_to(ti, t.shape[: -len(ti.shape)] + ti.shape) yi_ex = jnp.broadcast_to(yi, y.shape[: -len(yi.shape)] + yi.shape) to = jnp.concatenate([t, ti_ex], axis=-1) yo = jnp.concatenate([y, yi_ex], axis=-1) # Sort the spline according to t. sort_idx = jnp.argsort(to) to = jnp.take_along_axis(to, sort_idx, axis=-1) yo = jnp.take_along_axis(yo, sort_idx, axis=-1) return to, yo def clamp(t, y, minval, maxval): """Clamp (t, y) to be zero outside of t in [minval, maxval].""" utils.assert_valid_linspline(t, y) check_zero_endpoints(y) # Add in extra points at and immediately above/below the min/max vals. ti = jnp.concatenate( [ math.minus_eps(minval), minval, maxval, math.plus_eps(maxval), ], axis=-1, ) tc, yo = insert_knot(ti, t, y) # Zero the spline values outside of [minval, maxval]. yc = jnp.where(tc > maxval, 0, jnp.where(tc < minval, 0, yo)) return tc, yc def compute_integral(t, y): """Integrate<fim_suffix><fim_middle> a linear spline into a piecewise quadratic spline."""
a linear spline into a piecewise quadratic spline."""
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/ref_utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Functions for reflection directions and directional encodings.""" import math from internal import math as math_lib import jax.numpy as jnp import numpy as np def reflect(viewdirs, normals): """Reflect view directions about normals. The reflection of a vector v about a unit vector n is a vector u such that dot(v, n) = dot(u, n), and dot(u, u) = dot(v, v). The solution to these two equations is u = 2 dot(n, v) n - v. Args: viewdirs: [..., 3] array of view directions. normals: [..., 3] array of normal directions (assumed to be unit vectors). Returns: [..., 3] array of reflection directions. """ return ( 2.0 * jnp.sum(normals * viewdirs, axis=-1, keepdims=True) * normals - viewdirs ) def l2_normalize(x, grad_eps=jnp.finfo(jnp.float32).eps): """Normalize x to unit length along last axis. Normalizing vectors is surprisingly tricky, because you have to address the case where the denominator in the normalization is tiny or zero, in which case gradients will explode. For this reason, we perform two normalizations: in the forward pass, we clamp the denominator with ~1e-40, but in the backward pass we clamp with `grad_eps`, which defaults to ~1e-7. This guarantees that the output of this function is unit norm (unless x is very very small) while preventing exploding gradients. Args: x: The array of values to normalize. grad_eps: The value to clip the squared norm by before division in the backward pass. Returns: A normalized array x / ||x||, normalized along the last axis. """ tiny = jnp.finfo(jnp.float32).tiny grad_eps = jnp.maximum(tiny, grad_eps) denom_sq = jnp.sum(x**2, axis=-1, keepdims=True) normal_val = x / jnp.sqrt(jnp.maximum(tiny, denom_sq)) normal_grad = x / jnp.sqrt(jnp.maximum(grad_eps, denom_sq)) # Use `normal_val` in the forward pass but `normal_grad` in the backward pass. normal = math_lib.override_gradient(normal_val, normal_grad) return jnp.where(denom_sq < tiny, jnp.zeros_like(normal), normal) def compute_weighted_mae(weights, normals, normals_gt): """Compute weighted mean angular error, assuming normals are unit length.""" angles = math_lib.safe_arccos((normals * normals_gt).sum(axis=-1)) return (180.0 / jnp.pi) * ((weights * angles).sum() / weights.sum()) def generalized_binomial_coeff(a, k): """Compute generalized binomial coefficients.""" return np.prod(a - np.arange(k)) / math.factorial(k) def assoc_legendre_coeff(l, m, k): """Compute associated Legendre polynomial coefficients. Returns the coefficient of the cos^k(theta)*sin^m(theta) term in the (l, m)th associated Legendre polynomial, P_l^m(cos(theta)). Args: l: associated Legendre polynomial degree. m: associated Legendre polynomial order. k: power of cos(theta). Returns: A float, the coefficient of the term corresponding to the inputs. """ return ( (-1) ** m * 2**l * math.factorial(l) / math.factorial(k) / math.factorial(l - k - m) * generalized_binomial_coeff(0.5 * (l + k + m - 1.0), l) ) def sph_harm_coeff(l, m, k): """Compute<fim_suffix><fim_middle> spherical harmonic coefficients."""
spherical harmonic coefficients."""
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/math.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Mathy utility functions.""" import functools import jax import jax.numpy as jnp import numpy as np tiny_val = np.float32(np.finfo(np.float32).tiny) min_val = np.float32(np.finfo(np.float32).min) max_val = np.float32(np.finfo(np.float32).max) def laplace_cdf(x, beta): alpha = 1 / beta return alpha * (0.5 + 0.5 * safe_sign(x) * (jnp.exp(-jnp.abs(x) / beta) - 1)) def scaled_softplus(x, scale=100.0): return (1.0 / scale) * jax.nn.softplus(scale * x) def matmul(a, b): """jnp.matmul defaults to bfloat16, but this helper function doesn't.""" return jnp.matmul(a, b, precision=jax.lax.Precision.HIGHEST) def unstack(x, axis=0): return tuple( jnp.squeeze(z, axis=axis) for z in jnp.split(x, x.shape[axis], axis=axis) ) @jax.custom_jvp def plus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, tiny_val, jnp.nextafter(jnp.float32(x), jnp.inf) ) @jax.custom_jvp def minus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, -tiny_val, jnp.nextafter(jnp.float32(x), -jnp.inf) ) @plus_eps.defjvp def plus_eps_jvp(primals, tangents): """Make plus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return plus_eps(*primals), tangents[0] @minus_eps.defjvp def minus_eps_jvp(primals, tangents): """Make minus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return minus_eps(*primals), tangents[0] @jax.custom_jvp def expm1(x): """jnp.expm1() has inaccurate gradients when x << 0, this doesn't.""" return jnp.expm1(x) @expm1.defjvp def expm1_jvp(primals, tangents): return expm1(*primals), tangents[0] * jnp.exp(primals[0]) def safe_trig_helper(x, fn, t=100 * jnp.pi): """Helper function used by safe_cos/safe_sin: mods x before sin()/cos().""" return fn(jnp.nan_to_num(jnp.where(jnp.abs(x) < t, x, x % t))) def safe_cos(x): """jnp.cos() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.cos) def safe_sin(x): """jnp.sin() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.sin) @jax.custom_vjp def safe_arctan2(x1, x2): return safe_arctan2_fwd(x1, x2)[0] def safe_arctan2_fwd(x1, x2): return jnp.arctan2(x1, x2), (x1, x2) def safe_arctan2_bwd(res, g): x1, x2 = res denom = remove_zero(x1**2 + x2**2) d1 = g * (x2 / denom) d2 = g * (-x1 / denom) return d1, d2 safe_arctan2.defvjp(safe_arctan2_fwd, safe_arctan2_bwd) def generate_clip_nograd_fn(a_min, a_max): """Generates a function that clips to [a_min, a_max] with no grad effects.""" @jax.custom_jvp def clip_nograd(a): """Clamps `a` from above and below.""" return jnp.clip(a, a_min, a_max) @clip_nograd.defjvp def clip_nograd_jvp(primals, tangents): """Override clips()'s gradient to be a no-op.""" return clip_nograd(primals[0]), tangents[0] return clip_nograd clip_finite_nograd = generate_clip_nograd_fn(min_val, max_val) clip_pos_finite_nograd = generate_clip_nograd_fn(tiny_val, max_val) def clip_pos(x): """Clamps `x` from below to be positive.""" return jnp.maximum(tiny_val, x) def safe_sign(x): """jnp.sign(x) except x=0 is assumed to have a sign of +1, not 0.""" return jnp.where(x < 0, -1, +1) def remove_zero(x): """Shifts `x` away from 0.""" return jnp.where(jnp.abs(x) < tiny_val, tiny_val, x) def clip_finite(x): return jnp.clip(x, min_val, max_val) @jax.custom_vjp def safe_div(n, d): """Divide `n` by `d` but the value and gradient never nan out.""" return safe_div_fwd(n, d)[0] def safe_div_fwd(n, d): r = jnp.clip(n / remove_zero(d), min_val, max_val) return jnp.where(jnp.abs(d) < tiny_val, 0, r), (d, r) def safe_div_bwd(res, g): d, r = res dn = jnp.clip(g / remove_zero(d), min_val, max_val) dd = jnp.clip(-g * r / remove_zero(d), min_val, max_val) return dn, dd safe_div.defvjp(safe_div_fwd, safe_div_bwd) def generate_safe_fn(fn, grad_fn, x_range): """Generate's a `safe` fn() where inputs are clipped in fwd and bwd passes.""" @jax.custom_jvp def safe_fn(x): """fn() with clipped inputs.""" return fn(jnp.clip(x, *x_range)) @safe_fn.defjvp def safe_fn_jvp(primals, tangents): """Backpropagate<fim_suffix><fim_middle> using the gradient and clipped inputs."""
using the gradient and clipped inputs."""
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/render.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Helper functions for shooting and rendering rays.""" import jax import jax.numpy as jnp import jax.scipy as jsp from internal import math from internal import stepfun def lift_gaussian(d, t_mean, t_var, r_var, diag): """Lift a Gaussian defined along a ray to 3D coordinates.""" mean = d[Ellipsis, None, :] * t_mean[Ellipsis, None] d_mag_sq = jnp.maximum(1e-10, jnp.sum(d**2, axis=-1, keepdims=True)) if diag: d_outer_diag = d**2 null_outer_diag = 1 - d_outer_diag / d_mag_sq t_cov_diag = t_var[Ellipsis, None] * d_outer_diag[Ellipsis, None, :] xy_cov_diag = r_var[Ellipsis, None] * null_outer_diag[Ellipsis, None, :] cov_diag = t_cov_diag + xy_cov_diag return mean, cov_diag else: d_outer = d[Ellipsis, :, None] * d[Ellipsis, None, :] eye = jnp.eye(d.shape[-1]) null_outer = eye - d[Ellipsis, :, None] * (d / d_mag_sq)[Ellipsis, None, :] t_cov = t_var[Ellipsis, None, None] * d_outer[Ellipsis, None, :, :] xy_cov = r_var[Ellipsis, None, None] * null_outer[Ellipsis, None, :, :] cov = t_cov + xy_cov return mean, cov def gaussianize_frustum(t0, t1): """Convert intervals along a conical frustum into means and variances.""" # A more stable version of Equation 7 from https://arxiv.org/abs/2103.13415. s = t0 + t1 d = t1 - t0 eps = jnp.finfo(jnp.float32).eps ** 2 ratio = d**2 / jnp.maximum(eps, 3 * s**2 + d**2) t_mean = s * (1 / 2 + ratio) t_var = (1 / 12) * d**2 - (1 / 15) * ratio**2 * (12 * s**2 - d**2) r_var = (1 / 16) * s**2 + d**2 * (5 / 48 - (1 / 15) * ratio) return t_mean, t_var, r_var def conical_frustum_to_gaussian(d, t0, t1, base_radius, diag): """Approximate a 3D conical frustum as a Gaussian distribution (mean+cov). Assumes the ray is originating from the origin, and base_radius is the radius at dist=1. Doesn't assume `d` is normalized. Args: d: jnp.float32 3-vector, the axis of the cone t0: float, the starting distance of the frustum. t1: float, the ending distance of the frustum. base_radius: float, the scale of the radius as a function of distance. diag: boolean, whether or the Gaussian will be diagonal or full-covariance. Returns: a Gaussian (mean and covariance). """ t_mean, t_var, r_var = gaussianize_frustum(t0, t1) r_var *= base_radius**2 mean, cov = lift_gaussian(d, t_mean, t_var, r_var, diag) return mean, cov def cylinder_to_gaussian(d, t0, t1, radius, diag): """Approximate<fim_suffix><fim_middle> a cylinder as a Gaussian distribution (mean+cov). Assumes the ray is originating from the origin, and radius is the radius. Does not renormalize `d`. Args: d: jnp.float32 3-vector, the axis of the cylinder t0: float, the starting distance of the cylinder. t1: float, the ending distance of the cylinder. radius: float, the radius of the cylinder diag: boolean, whether or the Gaussian will be diagonal or full-covariance. Returns: a Gaussian (mean and covariance). """
a cylinder as a Gaussian distribution (mean+cov). Assumes the ray is originating from the origin, and radius is the radius. Does not renormalize `d`. Args: d: jnp.float32 3-vector, the axis of the cylinder t0: float, the starting distance of the cylinder. t1: float, the ending distance of the cylinder. radius: float, the radius of the cylinder diag: boolean, whether or the Gaussian will be diagonal or full-covariance. Returns: a Gaussian (mean and covariance). """
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/coord.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating coordinate spaces and distances along rays.""" from internal import geopoly from internal import math import jax from jax import random import jax.numpy as jnp import numpy as np def contract(x): """Contracts points towards the origin (Eq 10 of arxiv.org/abs/2111.12077).""" # Clamping to 1 produces correct scale inside |x| < 1 x_mag_sq = jnp.maximum(1, jnp.sum(x**2, axis=-1, keepdims=True)) scale = (2 * jnp.sqrt(x_mag_sq) - 1) / x_mag_sq z = scale * x return z def inv_contract(z): """The inverse of contract().""" # Clamping to 1 produces correct scale inside |z| < 1 z_mag_sq = jnp.maximum(1, jnp.sum(z**2, axis=-1, keepdims=True)) inv_scale = 2 * jnp.sqrt(z_mag_sq) - z_mag_sq x = z / inv_scale return x def track_linearize(fn, mean, cov): """Apply function `fn` to a set of means and covariances, ala a Kalman filter. We can analytically transform a Gaussian parameterized by `mean` and `cov` with a function `fn` by linearizing `fn` around `mean`, and taking advantage of the fact that Covar[Ax + y] = A(Covar[x])A^T (see https://cs.nyu.edu/~roweis/notes/gaussid.pdf for details). Args: fn: A function that can be applied to `mean`. mean: a tensor of Gaussian means, where the last axis is the dimension. cov: a tensor of covariances, where the last two axes are the dimensions. Returns: fn_mean: the transformed means. fn_cov: the transformed covariances. """ if (len(mean.shape) + 1) != len(cov.shape): raise ValueError('cov must be non-diagonal') fn_mean, lin_fn = jax.linearize(fn, mean) fn_cov = jax.vmap(lin_fn, -1, -2)(jax.vmap(lin_fn, -1, -2)(cov)) return fn_mean, fn_cov def track_isotropic(fn, mean, scale): """Apply function `fn` to a set of means and scales, ala a Kalman filter. This is the isotropic or scalar equivalent of track_linearize, as we're still linearizing a function and tracking a Gaussian through it, but the input and output Gaussians are all isotropic and are only represented with a single `scale` value (where `scale**2` is the variance of the Gaussian). Args: fn: A function that can be applied to `mean`. mean: a tensor of Gaussian means, where the last axis is the dimension. scale: a tensor of scales, with the same shape as means[..., -1]. Returns: fn_mean: the transformed means. fn_scale: the transformed scales. """ if mean.shape[:-1] != scale.shape: raise ValueError( f'mean.shape[:-1] {mean.shape}[:-1] != scale.shape {scale.shape}.' ) d = mean.shape[-1] fn_mean, lin_fn = jax.linearize(fn, mean) if scale is not None: # Compute the Jacobian of fn function at the locations of each mean. jac = jax.vmap(lin_fn, in_axes=-1, out_axes=-1)( jnp.broadcast_to(jnp.eye(d), mean.shape + (d,)) ) # The cube root of the determinant of the Jacobian is the geometric mean # of the eigenvalues of the Jacobian, which gives us the isotropic scaling # implied by `fn` at each mean that `scale` should be multiplied by. eps = jnp.finfo(jnp.float32).tiny # Guard against an inf gradient at 0. abs_det = jnp.maximum(eps, jnp.abs(jnp.linalg.det(jac))) # Special case d == 3 for speed's sake. fn_scale = scale * (jnp.cbrt(abs_det) if d == 3 else abs_det ** (1 / d)) else: fn_scale = None return fn_mean, fn_scale def contract3_isoscale(x): """A fast version of track_isotropic(contract, *)'s scaling for 3D inputs.""" if x.shape[-1] != 3: raise ValueError(f'Inputs must be 3D, are {x.shape[-1]}D.') norm_sq = jnp.maximum(1, jnp.sum(x**2, axis=-1)) # Equivalent to cbrt((2 * sqrt(norm_sq) - 1) ** 2) / norm_sq: return jnp.exp(2 / 3 * jnp.log(2 * jnp.sqrt(norm_sq) - 1) - jnp.log(norm_sq)) def construct_ray_warps(fn, t_near, t_far, *, fn_inv=None): """Construct a bijection between metric distances and normalized distances. See the text around Equation 11 in https://arxiv.org/abs/2111.12077 for a detailed explanation. Args: fn: the function to ray distances. t_near: a tensor of near-plane distances. t_far: a tensor of far-plane distances. fn_inv: Optional, if not None then it's used as the inverse of fn(). Returns: t_to_s: a function that maps distances to normalized distances in [0, 1]. s_to_t: the inverse of t_to_s. """ if fn is None: fn_fwd = lambda x: x fn_inv = lambda x: x else: fn_fwd = fn if fn_inv is None: # A simple mapping from some functions to their inverse. inv_mapping = { 'reciprocal': jnp.reciprocal, 'log': jnp.exp, 'exp': jnp.log, 'sqrt': jnp.square, 'square': jnp.sqrt, } fn_inv = inv_mapping[fn.__name__] fn_t_near, fn_t_far = [fn_fwd(t) for t in (t_near, t_far)] # Forcibly clip t to the range of valid values, to guard against inf's. t_clip = lambda t: jnp.clip(t, t_near, t_far) t_to_s = lambda t: (fn_fwd(t_clip(t)) - fn_t_near) / (fn_t_far - fn_t_near) s_to_t = lambda s: t_clip(fn_inv(s * fn_t_far + (1 - s) * fn_t_near)) return t_to_s, s_to_t def expected_sin(mean, var): """Compute<fim_suffix><fim_middle> the mean of sin(x), x ~ N(mean, var)."""
the mean of sin(x), x ~ N(mean, var)."""
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/math.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Mathy utility functions.""" import functools import jax import jax.numpy as jnp import numpy as np tiny_val = np.float32(np.finfo(np.float32).tiny) min_val = np.float32(np.finfo(np.float32).min) max_val = np.float32(np.finfo(np.float32).max) def laplace_cdf(x, beta): alpha = 1 / beta return alpha * (0.5 + 0.5 * safe_sign(x) * (jnp.exp(-jnp.abs(x) / beta) - 1)) def scaled_softplus(x, scale=100.0): return (1.0 / scale) * jax.nn.softplus(scale * x) def matmul(a, b): """jnp.matmul defaults to bfloat16, but this helper function doesn't.""" return jnp.matmul(a, b, precision=jax.lax.Precision.HIGHEST) def unstack(x, axis=0): return tuple( jnp.squeeze(z, axis=axis) for z in jnp.split(x, x.shape[axis], axis=axis) ) @jax.custom_jvp def plus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, tiny_val, jnp.nextafter(jnp.float32(x), jnp.inf) ) @jax.custom_jvp def minus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, -tiny_val, jnp.nextafter(jnp.float32(x), -jnp.inf) ) @plus_eps.defjvp def plus_eps_jvp(primals, tangents): """Make plus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return plus_eps(*primals), tangents[0] @minus_eps.defjvp def minus_eps_jvp(primals, tangents): """Make minus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return minus_eps(*primals), tangents[0] @jax.custom_jvp def expm1(x): """jnp.expm1() has inaccurate gradients when x << 0, this doesn't.""" return jnp.expm1(x) @expm1.defjvp def expm1_jvp(primals, tangents): return expm1(*primals), tangents[0] * jnp.exp(primals[0]) def safe_trig_helper(x, fn, t=100 * jnp.pi): """Helper function used by safe_cos/safe_sin: mods x before sin()/cos().""" return fn(jnp.nan_to_num(jnp.where(jnp.abs(x) < t, x, x % t))) def safe_cos(x): """jnp.cos() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.cos) def safe_sin(x): """jnp.sin() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.sin) @jax.custom_vjp def safe_arctan2(x1, x2): return safe_arctan2_fwd(x1, x2)[0] def safe_arctan2_fwd(x1, x2): return jnp.arctan2(x1, x2), (x1, x2) def safe_arctan2_bwd(res, g): x1, x2 = res denom = remove_zero(x1**2 + x2**2) d1 = g * (x2 / denom) d2 = g * (-x1 / denom) return d1, d2 safe_arctan2.defvjp(safe_arctan2_fwd, safe_arctan2_bwd) def generate_clip_nograd_fn(a_min, a_max): """Generates a function that clips to [a_min, a_max] with no grad effects.""" @jax.custom_jvp def clip_nograd(a): """Clamps `a` from above and below.""" return jnp.clip(a, a_min, a_max) @clip_nograd.defjvp def clip_nograd_jvp(primals, tangents): """Override clips()'s gradient to be a no-op.""" return clip_nograd(primals[0]), tangents[0] return clip_nograd clip_finite_nograd = generate_clip_nograd_fn(min_val, max_val) clip_pos_finite_nograd = generate_clip_nograd_fn(tiny_val, max_val) def clip_pos(x): """Clamps<fim_suffix><fim_middle> `x` from below to be positive."""
`x` from below to be positive."""
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/stepfun.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating step functions (piecewise-constant 1D functions). We have a shared naming and dimension convention for these functions. All input/output step functions are assumed to be aligned along the last axis. `t` always indicates the x coordinates of the *endpoints* of a step function. `y` indicates unconstrained values for the *bins* of a step function `w` indicates bin weights that sum to <= 1. `p` indicates non-negative bin values that *integrate* to <= 1. """ from internal import linspline from internal import math from internal import utils import jax import jax.numpy as jnp import numpy as np def query(tq, t, y, left=None, right=None): """Query step function (t, y) at locations tq. Edges repeat by default.""" utils.assert_valid_stepfun(t, y) # Query the step function to recover the interval value. (i0, i1), ((yq, _),) = math.sorted_lookup(tq, t, (y,), utils.device_is_tpu()) # Apply boundary conditions. left = y[Ellipsis, :1] if left is None else left right = y[Ellipsis, -1:] if right is None else right yq = math.select([(i1 == 0, left), (i0 == y.shape[-1], right)], yq) return yq def weight_to_pdf(t, w): """Turn a vector of weights that sums to 1 into a PDF that integrates to 1.""" utils.assert_valid_stepfun(t, w) td = jnp.diff(t) return jnp.where(td < np.finfo(np.float32).tiny, 0, math.safe_div(w, td)) def pdf_to_weight(t, p): """Turn a PDF that integrates to 1 into a vector of weights that sums to 1.""" utils.assert_valid_stepfun(t, p) return p * jnp.diff(t) def integrate_weights(w): """Compute the cumulative sum of w, assuming all weight vectors sum to 1. The output's size on the last dimension is one greater than that of the input, because we're computing the integral corresponding to the endpoints of a step function, not the integral of the interior/bin values. Args: w: Tensor, which will be integrated along the last axis. This is assumed to sum to 1 along the last axis, and this function will (silently) break if that is not the case. Returns: cw0: Tensor, the integral of w, where cw0[..., 0] = 0 and cw0[..., -1] = 1 """ cw = jnp.minimum(1, jnp.cumsum(w[Ellipsis, :-1], axis=-1)) shape = cw.shape[:-1] + (1,) # Ensure that the CDF starts with exactly 0 and ends with exactly 1. cw0 = jnp.concatenate([jnp.zeros(shape), cw, jnp.ones(shape)], axis=-1) return cw0 def invert_cdf(u, t, w_logits): """Invert the CDF defined by (t, w) at the points specified by u in [0, 1).""" utils.assert_valid_stepfun(t, w_logits) # Compute the PDF and CDF for each weight vector. w = jax.nn.softmax(w_logits, axis=-1) cw = integrate_weights(w) # Interpolate into the inverse CDF. t_new = math.sorted_interp(u, cw, t, utils.device_is_tpu()) return t_new def sample( rng, t, w_logits, num_samples, single_jitter=False, deterministic_center=False, eps=jnp.finfo(jnp.float32).eps, ): """Piecewise-Constant PDF sampling from a step function. Args: rng: random number generator (or None for `linspace` sampling). t: [..., num_bins + 1], bin endpoint coordinates (must be sorted) w_logits: [..., num_bins], logits corresponding to bin weights num_samples: int, the number of samples. single_jitter: bool, if True, jitter every sample along each ray by the same amount in the inverse CDF. Otherwise, jitter each sample independently. deterministic_center: bool, if False, when `rng` is None return samples that linspace the entire PDF. If True, skip the front and back of the linspace so that the centers of each PDF interval are returned. eps: float, something like numerical epsilon. Returns: t_samples: jnp.ndarray(float32), [batch_size, num_samples]. """ utils.assert_valid_stepfun(t, w_logits) # Draw uniform samples. if rng is None: # Match the behavior of jax.random.uniform() by spanning [0, 1-eps]. if<fim_suffix><fim_middle> deterministic_center: pad = 1 / (2 * num_samples) u = jnp.linspace(pad, 1.0 - pad - eps, num_samples) else: u = jnp.linspace(0, 1.0 - eps, num_samples)
deterministic_center: pad = 1 / (2 * num_samples) u = jnp.linspace(pad, 1.0 - pad - eps, num_samples) else: u = jnp.linspace(0, 1.0 - eps, num_samples)
IF
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/coord.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating coordinate spaces and distances along rays.""" from internal import geopoly from internal import math import jax from jax import random import jax.numpy as jnp import numpy as np def contract(x): """Contracts points towards the origin (Eq 10 of arxiv.org/abs/2111.12077).""" # Clamping to 1 produces correct scale inside |x| < 1 x_mag_sq = jnp.maximum(1, jnp.sum(x**2, axis=-1, keepdims=True)) scale = (2 * jnp.sqrt(x_mag_sq) - 1) / x_mag_sq z = scale * x return z def inv_contract(z): """The inverse of contract().""" # Clamping to 1 produces correct scale inside |z| < 1 z_mag_sq = jnp.maximum(1, jnp.sum(z**2, axis=-1, keepdims=True)) inv_scale = 2 * jnp.sqrt(z_mag_sq) - z_mag_sq x = z / inv_scale return x def track_linearize(fn, mean, cov): """Apply function `fn` to a set of means and covariances, ala a Kalman filter. We can analytically transform a Gaussian parameterized by `mean` and `cov` with a function `fn` by linearizing `fn` around `mean`, and taking advantage of the fact that Covar[Ax + y] = A(Covar[x])A^T (see https://cs.nyu.edu/~roweis/notes/gaussid.pdf for details). Args: fn: A function that can be applied to `mean`. mean: a tensor of Gaussian means, where the last axis is the dimension. cov: a tensor of covariances, where the last two axes are the dimensions. Returns: fn_mean: the transformed means. fn_cov: the transformed covariances. """ if (len(mean.shape) + 1) != len(cov.shape): raise ValueError('cov must be non-diagonal') fn_mean, lin_fn = jax.linearize(fn, mean) fn_cov = jax.vmap(lin_fn, -1, -2)(jax.vmap(lin_fn, -1, -2)(cov)) return fn_mean, fn_cov def track_isotropic(fn, mean, scale): """Apply function `fn` to a set of means and scales, ala a Kalman filter. This is the isotropic or scalar equivalent of track_linearize, as we're still linearizing a function and tracking a Gaussian through it, but the input and output Gaussians are all isotropic and are only represented with a single `scale` value (where `scale**2` is the variance of the Gaussian). Args: fn: A function that can be applied to `mean`. mean: a tensor of Gaussian means, where the last axis is the dimension. scale: a tensor of scales, with the same shape as means[..., -1]. Returns: fn_mean: the transformed means. fn_scale: the transformed scales. """ if mean.shape[:-1] != scale.shape: raise ValueError( f'mean.shape[:-1] {mean.shape}[:-1] != scale.shape {scale.shape}.' ) d = mean.shape[-1] fn_mean, lin_fn = jax.linearize(fn, mean) if<fim_suffix><fim_middle> scale is not None: # Compute the Jacobian of fn function at the locations of each mean. jac = jax.vmap(lin_fn, in_axes=-1, out_axes=-1)( jnp.broadcast_to(jnp.eye(d), mean.shape + (d,)) ) # The cube root of the determinant of the Jacobian is the geometric mean # of the eigenvalues of the Jacobian, which gives us the isotropic scaling # implied by `fn` at each mean that `scale` should be multiplied by. eps = jnp.finfo(jnp.float32).tiny # Guard against an inf gradient at 0. abs_det = jnp.maximum(eps, jnp.abs(jnp.linalg.det(jac))) # Special case d == 3 for speed's sake. fn_scale = scale * (jnp.cbrt(abs_det) if d == 3 else abs_det ** (1 / d)) else: fn_scale = None
scale is not None: # Compute the Jacobian of fn function at the locations of each mean. jac = jax.vmap(lin_fn, in_axes=-1, out_axes=-1)( jnp.broadcast_to(jnp.eye(d), mean.shape + (d,)) ) # The cube root of the determinant of the Jacobian is the geometric mean # of the eigenvalues of the Jacobian, which gives us the isotropic scaling # implied by `fn` at each mean that `scale` should be multiplied by. eps = jnp.finfo(jnp.float32).tiny # Guard against an inf gradient at 0. abs_det = jnp.maximum(eps, jnp.abs(jnp.linalg.det(jac))) # Special case d == 3 for speed's sake. fn_scale = scale * (jnp.cbrt(abs_det) if d == 3 else abs_det ** (1 / d)) else: fn_scale = None
IF
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/stepfun.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating step functions (piecewise-constant 1D functions). We have a shared naming and dimension convention for these functions. All input/output step functions are assumed to be aligned along the last axis. `t` always indicates the x coordinates of the *endpoints* of a step function. `y` indicates unconstrained values for the *bins* of a step function `w` indicates bin weights that sum to <= 1. `p` indicates non-negative bin values that *integrate* to <= 1. """ from internal import linspline from internal import math from internal import utils import jax import jax.numpy as jnp import numpy as np def query(tq, t, y, left=None, right=None): """Query step function (t, y) at locations tq. Edges repeat by default.""" utils.assert_valid_stepfun(t, y) # Query the step function to recover the interval value. (i0, i1), ((yq, _),) = math.sorted_lookup(tq, t, (y,), utils.device_is_tpu()) # Apply boundary conditions. left = y[Ellipsis, :1] if left is None else left right = y[Ellipsis, -1:] if right is None else right yq = math.select([(i1 == 0, left), (i0 == y.shape[-1], right)], yq) return yq def weight_to_pdf(t, w): """Turn a vector of weights that sums to 1 into a PDF that integrates to 1.""" utils.assert_valid_stepfun(t, w) td = jnp.diff(t) return jnp.where(td < np.finfo(np.float32).tiny, 0, math.safe_div(w, td)) def pdf_to_weight(t, p): """Turn a PDF that integrates to 1 into a vector of weights that sums to 1.""" utils.assert_valid_stepfun(t, p) return p * jnp.diff(t) def integrate_weights(w): """Compute the cumulative sum of w, assuming all weight vectors sum to 1. The output's size on the last dimension is one greater than that of the input, because we're computing the integral corresponding to the endpoints of a step function, not the integral of the interior/bin values. Args: w: Tensor, which will be integrated along the last axis. This is assumed to sum to 1 along the last axis, and this function will (silently) break if that is not the case. Returns: cw0: Tensor, the integral of w, where cw0[..., 0] = 0 and cw0[..., -1] = 1 """ cw = jnp.minimum(1, jnp.cumsum(w[Ellipsis, :-1], axis=-1)) shape = cw.shape[:-1] + (1,) # Ensure that the CDF starts with exactly 0 and ends with exactly 1. cw0 = jnp.concatenate([jnp.zeros(shape), cw, jnp.ones(shape)], axis=-1) return cw0 def invert_cdf(u, t, w_logits): """Invert the CDF defined by (t, w) at the points specified by u in [0, 1).""" utils.assert_valid_stepfun(t, w_logits) # Compute the PDF and CDF for each weight vector. w = jax.nn.softmax(w_logits, axis=-1) cw = integrate_weights(w) # Interpolate into the inverse CDF. t_new = math.sorted_interp(u, cw, t, utils.device_is_tpu()) return t_new def sample( rng, t, w_logits, num_samples, single_jitter=False, deterministic_center=False, eps=jnp.finfo(jnp.float32).eps, ): """Piecewise-Constant PDF sampling from a step function. Args: rng: random number generator (or None for `linspace` sampling). t: [..., num_bins + 1], bin endpoint coordinates (must be sorted) w_logits: [..., num_bins], logits corresponding to bin weights num_samples: int, the number of samples. single_jitter: bool, if True, jitter every sample along each ray by the same amount in the inverse CDF. Otherwise, jitter each sample independently. deterministic_center: bool, if False, when `rng` is None return samples that linspace the entire PDF. If True, skip the front and back of the linspace so that the centers of each PDF interval are returned. eps: float, something like numerical epsilon. Returns: t_samples: jnp.ndarray(float32), [batch_size, num_samples]. """ utils.assert_valid_stepfun(t, w_logits) # Draw uniform samples. if<fim_suffix><fim_middle> rng is None: # Match the behavior of jax.random.uniform() by spanning [0, 1-eps]. if deterministic_center: pad = 1 / (2 * num_samples) u = jnp.linspace(pad, 1.0 - pad - eps, num_samples) else: u = jnp.linspace(0, 1.0 - eps, num_samples) u = jnp.broadcast_to(u, t.shape[:-1] + (num_samples,)) else: # `u` is in [0, 1) --- it can be zero, but it can never be 1. u_max = eps + (1 - eps) / num_samples max_jitter = (1 - u_max) / (num_samples - 1) - eps d = 1 if single_jitter else num_samples u = jnp.linspace(0, 1 - u_max, num_samples) + jax.random.uniform( rng, t.shape[:-1] + (d,), maxval=max_jitter )
rng is None: # Match the behavior of jax.random.uniform() by spanning [0, 1-eps]. if deterministic_center: pad = 1 / (2 * num_samples) u = jnp.linspace(pad, 1.0 - pad - eps, num_samples) else: u = jnp.linspace(0, 1.0 - eps, num_samples) u = jnp.broadcast_to(u, t.shape[:-1] + (num_samples,)) else: # `u` is in [0, 1) --- it can be zero, but it can never be 1. u_max = eps + (1 - eps) / num_samples max_jitter = (1 - u_max) / (num_samples - 1) - eps d = 1 if single_jitter else num_samples u = jnp.linspace(0, 1 - u_max, num_samples) + jax.random.uniform( rng, t.shape[:-1] + (d,), maxval=max_jitter )
IF
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if<fim_suffix><fim_middle> not populating_data and results_queue.empty(): break
not populating_data and results_queue.empty(): break
IF
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/coord.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating coordinate spaces and distances along rays.""" from internal import geopoly from internal import math import jax from jax import random import jax.numpy as jnp import numpy as np def contract(x): """Contracts points towards the origin (Eq 10 of arxiv.org/abs/2111.12077).""" # Clamping to 1 produces correct scale inside |x| < 1 x_mag_sq = jnp.maximum(1, jnp.sum(x**2, axis=-1, keepdims=True)) scale = (2 * jnp.sqrt(x_mag_sq) - 1) / x_mag_sq z = scale * x return z def inv_contract(z): """The inverse of contract().""" # Clamping to 1 produces correct scale inside |z| < 1 z_mag_sq = jnp.maximum(1, jnp.sum(z**2, axis=-1, keepdims=True)) inv_scale = 2 * jnp.sqrt(z_mag_sq) - z_mag_sq x = z / inv_scale return x def track_linearize(fn, mean, cov): """Apply function `fn` to a set of means and covariances, ala a Kalman filter. We can analytically transform a Gaussian parameterized by `mean` and `cov` with a function `fn` by linearizing `fn` around `mean`, and taking advantage of the fact that Covar[Ax + y] = A(Covar[x])A^T (see https://cs.nyu.edu/~roweis/notes/gaussid.pdf for details). Args: fn: A function that can be applied to `mean`. mean: a tensor of Gaussian means, where the last axis is the dimension. cov: a tensor of covariances, where the last two axes are the dimensions. Returns: fn_mean: the transformed means. fn_cov: the transformed covariances. """ if (len(mean.shape) + 1) != len(cov.shape): raise ValueError('cov must be non-diagonal') fn_mean, lin_fn = jax.linearize(fn, mean) fn_cov = jax.vmap(lin_fn, -1, -2)(jax.vmap(lin_fn, -1, -2)(cov)) return fn_mean, fn_cov def track_isotropic(fn, mean, scale): """Apply function `fn` to a set of means and scales, ala a Kalman filter. This is the isotropic or scalar equivalent of track_linearize, as we're still linearizing a function and tracking a Gaussian through it, but the input and output Gaussians are all isotropic and are only represented with a single `scale` value (where `scale**2` is the variance of the Gaussian). Args: fn: A function that can be applied to `mean`. mean: a tensor of Gaussian means, where the last axis is the dimension. scale: a tensor of scales, with the same shape as means[..., -1]. Returns: fn_mean: the transformed means. fn_scale: the transformed scales. """ if mean.shape[:-1] != scale.shape: raise ValueError( f'mean.shape[:-1] {mean.shape}[:-1] != scale.shape {scale.shape}.' ) d = mean.shape[-1] fn_mean, lin_fn = jax.linearize(fn, mean) if scale is not None: # Compute the Jacobian of fn function at the locations of each mean. jac = jax.vmap(lin_fn, in_axes=-1, out_axes=-1)( jnp.broadcast_to(jnp.eye(d), mean.shape + (d,)) ) # The cube root of the determinant of the Jacobian is the geometric mean # of the eigenvalues of the Jacobian, which gives us the isotropic scaling # implied by `fn` at each mean that `scale` should be multiplied by. eps = jnp.finfo(jnp.float32).tiny # Guard against an inf gradient at 0. abs_det = jnp.maximum(eps, jnp.abs(jnp.linalg.det(jac))) # Special case d == 3 for speed's sake. fn_scale = scale * (jnp.cbrt(abs_det) if d == 3 else abs_det ** (1 / d)) else: fn_scale = None return fn_mean, fn_scale def contract3_isoscale(x): """A fast version of track_isotropic(contract, *)'s scaling for 3D inputs.""" if x.shape[-1] != 3: raise ValueError(f'Inputs must be 3D, are {x.shape[-1]}D.') norm_sq = jnp.maximum(1, jnp.sum(x**2, axis=-1)) # Equivalent to cbrt((2 * sqrt(norm_sq) - 1) ** 2) / norm_sq: return jnp.exp(2 / 3 * jnp.log(2 * jnp.sqrt(norm_sq) - 1) - jnp.log(norm_sq)) def construct_ray_warps(fn, t_near, t_far, *, fn_inv=None): """Construct a bijection between metric distances and normalized distances. See the text around Equation 11 in https://arxiv.org/abs/2111.12077 for a detailed explanation. Args: fn: the function to ray distances. t_near: a tensor of near-plane distances. t_far: a tensor of far-plane distances. fn_inv: Optional, if not None then it's used as the inverse of fn(). Returns: t_to_s: a function that maps distances to normalized distances in [0, 1]. s_to_t: the inverse of t_to_s. """ if fn is None: fn_fwd = lambda x: x fn_inv = lambda x: x else: fn_fwd = fn if fn_inv is None: # A simple mapping from some functions to their inverse. inv_mapping = { 'reciprocal': jnp.reciprocal, 'log': jnp.exp, 'exp': jnp.log, 'sqrt': jnp.square, 'square': jnp.sqrt, } fn_inv = inv_mapping[fn.__name__] fn_t_near, fn_t_far = [fn_fwd(t) for t in (t_near, t_far)] # Forcibly clip t to the range of valid values, to guard against inf's. t_clip = lambda t: jnp.clip(t, t_near, t_far) t_to_s = lambda t: (fn_fwd(t_clip(t)) - fn_t_near) / (fn_t_far - fn_t_near) s_to_t = lambda s: t_clip(fn_inv(s * fn_t_far + (1 - s) * fn_t_near)) return t_to_s, s_to_t def expected_sin(mean, var): """Compute the mean of sin(x), x ~ N(mean, var).""" return jnp.exp(-0.5 * var) * math.safe_sin(mean) # large var -> small value. def integrated_pos_enc(mean, var, min_deg, max_deg): """Encode `x` with sinusoids scaled by 2^[min_deg, max_deg). Args: mean: tensor, the mean coordinates to be encoded var: tensor, the variance of the coordinates to be encoded. min_deg: int, the min degree of the encoding. max_deg: int, the max degree of the encoding. Returns: encoded: jnp.ndarray, encoded variables. """ scales = 2.0 ** jnp.arange(min_deg, max_deg) shape = mean.shape[:-1] + (-1,) scaled_mean = jnp.reshape(mean[Ellipsis, None, :] * scales[:, None], shape) scaled_var = jnp.reshape(var[Ellipsis, None, :] * scales[:, None] ** 2, shape) return expected_sin( jnp.concatenate([scaled_mean, scaled_mean + 0.5 * jnp.pi], axis=-1), jnp.concatenate([scaled_var] * 2, axis=-1), ) def lift_and_diagonalize(mean, cov, basis): """Project `mean` and `cov` onto basis and diagonalize the projected cov.""" fn_mean = math.matmul(mean, basis) fn_cov_diag = jnp.sum(basis * math.matmul(cov, basis), axis=-2) return fn_mean, fn_cov_diag def pos_enc(x, min_deg, max_deg, append_identity=True): """The positional encoding used by the original NeRF paper.""" scales = 2.0 ** jnp.arange(min_deg, max_deg) shape = x.shape[:-1] + (-1,) scaled_x = x[Ellipsis, None, :] * scales[:, None] # (..., s, c). scaled_x = jnp.reshape(scaled_x, shape) # (..., s*c). # Note that we're not using safe_sin, unlike IPE. # (..., s*c + s*c). four_feat = jnp.sin( jnp.concatenate([scaled_x, scaled_x + 0.5 * jnp.pi], axis=-1) ) if append_identity: return jnp.concatenate([x, four_feat], axis=-1) else: return four_feat def sqrtm(mat, return_eigs=False): """Take the matrix square root of a PSD matrix [..., d, d].""" eigvec, eigval = jax.lax.linalg.eigh( mat, symmetrize_input=False, sort_eigenvalues=False ) scaling = math.safe_sqrt(eigval)[Ellipsis, None, :] sqrtm_mat = math.matmul(eigvec * scaling, jnp.moveaxis(eigvec, -2, -1)) return (sqrtm_mat, (eigvec, eigval)) if return_eigs else sqrtm_mat def isotropize(cov, mode='accurate'): """Turn covariances into isotropic covariances with the same determinant.""" d = cov.shape[-1] if d == 1: return cov if<fim_suffix><fim_middle> mode == 'fast': det = jnp.linalg.det(cov) diag_val = det ** (1 / d) is_invalid = (det <= jnp.finfo(jnp.float32).tiny) | ~jnp.isfinite(det) elif mode == 'accurate': log_det = jnp.linalg.slogdet(cov)[1] diag_val = jnp.exp(log_det / d) is_invalid = ~jnp.isfinite(log_det) else: raise ValueError(f'mode={mode} not implemented.')
mode == 'fast': det = jnp.linalg.det(cov) diag_val = det ** (1 / d) is_invalid = (det <= jnp.finfo(jnp.float32).tiny) | ~jnp.isfinite(det) elif mode == 'accurate': log_det = jnp.linalg.slogdet(cov)[1] diag_val = jnp.exp(log_det / d) is_invalid = ~jnp.isfinite(log_det) else: raise ValueError(f'mode={mode} not implemented.')
IF
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if<fim_suffix><fim_middle> not populating_data and results_queue.empty(): break
not populating_data and results_queue.empty(): break
IF
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/geopoly.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for constructing geodesic polyhedron, which are used as a basis.""" import itertools import numpy as np def compute_sq_dist(mat0, mat1=None): """Compute the squared Euclidean distance between all pairs of columns.""" if mat1 is None: mat1 = mat0 # Use the fact that ||x - y||^2 == ||x||^2 + ||y||^2 - 2 x^T y. sq_norm0 = np.sum(mat0**2, 0) sq_norm1 = np.sum(mat1**2, 0) sq_dist = sq_norm0[:, None] + sq_norm1[None, :] - 2 * mat0.T @ mat1 sq_dist = np.maximum(0, sq_dist) # Negative values must be numerical errors. return sq_dist def compute_tesselation_weights(v): """Tesselate the vertices of a triangle by a factor of `v`.""" if v < 1: raise ValueError(f'v {v} must be >= 1') int_weights = [] for i in range(v + 1): for j in range(v + 1 - i): int_weights.append((i, j, v - (i + j))) int_weights = np.array(int_weights) weights = int_weights / v # Barycentric weights. return weights def tesselate_geodesic(base_verts, base_faces, v, eps=1e-4): """Tesselate the vertices of a geodesic polyhedron. Args: base_verts: tensor of floats, the vertex coordinates of the geodesic. base_faces: tensor of ints, the indices of the vertices of base_verts that constitute eachface of the polyhedra. v: int, the factor of the tesselation (v==1 is a no-op). eps: float, a small value used to determine if two vertices are the same. Returns: verts: a tensor of floats, the coordinates of the tesselated vertices. """ if not isinstance(v, int): raise ValueError(f'v {v} must an integer') tri_weights = compute_tesselation_weights(v) verts = [] for base_face in base_faces: new_verts = np.matmul(tri_weights, base_verts[base_face, :]) new_verts /= np.sqrt(np.sum(new_verts**2, 1, keepdims=True)) verts.append(new_verts) verts = np.concatenate(verts, 0) sq_dist = compute_sq_dist(verts.T) assignment = np.array([np.min(np.argwhere(d <= eps)) for d in sq_dist]) unique = np.unique(assignment) verts = verts[unique, :] return verts def generate_basis( base_shape, angular_tesselation, remove_symmetries=True, eps=1e-4 ): """Generates a 3D basis by tesselating a geometric polyhedron. Args: base_shape: string, the name of the starting polyhedron, must be either 'tetrahedron', 'icosahedron' or 'octahedron'. angular_tesselation: int, the number of times to tesselate the polyhedron, must be >= 1 (a value of 1 is a no-op to the polyhedron). remove_symmetries: bool, if True then remove the symmetric basis columns, which is usually a good idea because otherwise projections onto the basis will have redundant negative copies of each other. eps: float, a small number used to determine symmetries. Returns: basis: a matrix with shape [3, n]. """ if base_shape == 'tetrahedron': verts = np.array([ (np.sqrt(8 / 9), 0, -1 / 3), (-np.sqrt(2 / 9), np.sqrt(2 / 3), -1 / 3), (-np.sqrt(2 / 9), -np.sqrt(2 / 3), -1 / 3), (0, 0, 1), ]) faces = np.array([(0, 1, 2), (0, 2, 3), (0, 1, 3), (1, 2, 3)]) elif base_shape == 'icosahedron': a = (np.sqrt(5) + 1) / 2 verts = np.array([ (-1, 0, a), (1, 0, a), (-1, 0, -a), (1, 0, -a), (0, a, 1), (0, a, -1), (0, -a, 1), (0, -a, -1), (a, 1, 0), (-a, 1, 0), (a, -1, 0), (-a, -1, 0), ]) / np.sqrt(a + 2) faces = np.array([ (0, 4, 1), (0, 9, 4), (9, 5, 4), (4, 5, 8), (4, 8, 1), (8, 10, 1), (8, 3, 10), (5, 3, 8), (5, 2, 3), (2, 7, 3), (7, 10, 3), (7, 6, 10), (7, 11, 6), (11, 0, 6), (0, 1, 6), (6, 1, 10), (9, 0, 11), (9, 11, 2), (9, 2, 5), (7, 2, 11), ]) elif base_shape == 'octahedron': verts = np.array( [(0, 0, -1), (0, 0, 1), (0, -1, 0), (0, 1, 0), (-1, 0, 0), (1, 0, 0)] ) corners = np.array(list(itertools.product([-1, 1], repeat=3))) pairs = np.argwhere(compute_sq_dist(corners.T, verts.T) == 2) faces = np.sort(np.reshape(pairs[:, 1], [3, -1]).T, 1) else: raise ValueError(f'base_shape {base_shape} not supported') verts = tesselate_geodesic(verts, faces, angular_tesselation) if<fim_suffix><fim_middle> remove_symmetries: # Remove elements of `verts` that are reflections of each other. match = compute_sq_dist(verts.T, -verts.T) < eps verts = verts[~np.any(np.triu(match), axis=0), :]
remove_symmetries: # Remove elements of `verts` that are reflections of each other. match = compute_sq_dist(verts.T, -verts.T) < eps verts = verts[~np.any(np.triu(match), axis=0), :]
IF
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/coord.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating coordinate spaces and distances along rays.""" from internal import geopoly from internal import math import jax from jax import random import jax.numpy as jnp import numpy as np def contract(x): """Contracts points towards the origin (Eq 10 of arxiv.org/abs/2111.12077).""" # Clamping to 1 produces correct scale inside |x| < 1 x_mag_sq = jnp.maximum(1, jnp.sum(x**2, axis=-1, keepdims=True)) scale = (2 * jnp.sqrt(x_mag_sq) - 1) / x_mag_sq z = scale * x return z def inv_contract(z): """The inverse of contract().""" # Clamping to 1 produces correct scale inside |z| < 1 z_mag_sq = jnp.maximum(1, jnp.sum(z**2, axis=-1, keepdims=True)) inv_scale = 2 * jnp.sqrt(z_mag_sq) - z_mag_sq x = z / inv_scale return x def track_linearize(fn, mean, cov): """Apply function `fn` to a set of means and covariances, ala a Kalman filter. We can analytically transform a Gaussian parameterized by `mean` and `cov` with a function `fn` by linearizing `fn` around `mean`, and taking advantage of the fact that Covar[Ax + y] = A(Covar[x])A^T (see https://cs.nyu.edu/~roweis/notes/gaussid.pdf for details). Args: fn: A function that can be applied to `mean`. mean: a tensor of Gaussian means, where the last axis is the dimension. cov: a tensor of covariances, where the last two axes are the dimensions. Returns: fn_mean: the transformed means. fn_cov: the transformed covariances. """ if (len(mean.shape) + 1) != len(cov.shape): raise ValueError('cov must be non-diagonal') fn_mean, lin_fn = jax.linearize(fn, mean) fn_cov = jax.vmap(lin_fn, -1, -2)(jax.vmap(lin_fn, -1, -2)(cov)) return fn_mean, fn_cov def track_isotropic(fn, mean, scale): """Apply function `fn` to a set of means and scales, ala a Kalman filter. This is the isotropic or scalar equivalent of track_linearize, as we're still linearizing a function and tracking a Gaussian through it, but the input and output Gaussians are all isotropic and are only represented with a single `scale` value (where `scale**2` is the variance of the Gaussian). Args: fn: A function that can be applied to `mean`. mean: a tensor of Gaussian means, where the last axis is the dimension. scale: a tensor of scales, with the same shape as means[..., -1]. Returns: fn_mean: the transformed means. fn_scale: the transformed scales. """ if mean.shape[:-1] != scale.shape: raise ValueError( f'mean.shape[:-1] {mean.shape}[:-1] != scale.shape {scale.shape}.' ) d = mean.shape[-1] fn_mean, lin_fn = jax.linearize(fn, mean) if scale is not None: # Compute the Jacobian of fn function at the locations of each mean. jac = jax.vmap(lin_fn, in_axes=-1, out_axes=-1)( jnp.broadcast_to(jnp.eye(d), mean.shape + (d,)) ) # The cube root of the determinant of the Jacobian is the geometric mean # of the eigenvalues of the Jacobian, which gives us the isotropic scaling # implied by `fn` at each mean that `scale` should be multiplied by. eps = jnp.finfo(jnp.float32).tiny # Guard against an inf gradient at 0. abs_det = jnp.maximum(eps, jnp.abs(jnp.linalg.det(jac))) # Special case d == 3 for speed's sake. fn_scale = scale * (jnp.cbrt(abs_det) if d == 3 else abs_det ** (1 / d)) else: fn_scale = None return fn_mean, fn_scale def contract3_isoscale(x): """A fast version of track_isotropic(contract, *)'s scaling for 3D inputs.""" if x.shape[-1] != 3: raise ValueError(f'Inputs must be 3D, are {x.shape[-1]}D.') norm_sq = jnp.maximum(1, jnp.sum(x**2, axis=-1)) # Equivalent to cbrt((2 * sqrt(norm_sq) - 1) ** 2) / norm_sq: return jnp.exp(2 / 3 * jnp.log(2 * jnp.sqrt(norm_sq) - 1) - jnp.log(norm_sq)) def construct_ray_warps(fn, t_near, t_far, *, fn_inv=None): """Construct a bijection between metric distances and normalized distances. See the text around Equation 11 in https://arxiv.org/abs/2111.12077 for a detailed explanation. Args: fn: the function to ray distances. t_near: a tensor of near-plane distances. t_far: a tensor of far-plane distances. fn_inv: Optional, if not None then it's used as the inverse of fn(). Returns: t_to_s: a function that maps distances to normalized distances in [0, 1]. s_to_t: the inverse of t_to_s. """ if fn is None: fn_fwd = lambda x: x fn_inv = lambda x: x else: fn_fwd = fn if<fim_suffix><fim_middle> fn_inv is None: # A simple mapping from some functions to their inverse. inv_mapping = { 'reciprocal': jnp.reciprocal, 'log': jnp.exp, 'exp': jnp.log, 'sqrt': jnp.square, 'square': jnp.sqrt, } fn_inv = inv_mapping[fn.__name__]
fn_inv is None: # A simple mapping from some functions to their inverse. inv_mapping = { 'reciprocal': jnp.reciprocal, 'log': jnp.exp, 'exp': jnp.log, 'sqrt': jnp.square, 'square': jnp.sqrt, } fn_inv = inv_mapping[fn.__name__]
IF
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/geopoly.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for constructing geodesic polyhedron, which are used as a basis.""" import itertools import numpy as np def compute_sq_dist(mat0, mat1=None): """Compute the squared Euclidean distance between all pairs of columns.""" if<fim_suffix><fim_middle> mat1 is None: mat1 = mat0
mat1 is None: mat1 = mat0
IF
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/math.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Mathy utility functions.""" import functools import jax import jax.numpy as jnp import numpy as np tiny_val = np.float32(np.finfo(np.float32).tiny) min_val = np.float32(np.finfo(np.float32).min) max_val = np.float32(np.finfo(np.float32).max) def laplace_cdf(x, beta): alpha = 1 / beta return alpha * (0.5 + 0.5 * safe_sign(x) * (jnp.exp(-jnp.abs(x) / beta) - 1)) def scaled_softplus(x, scale=100.0): return (1.0 / scale) * jax.nn.softplus(scale * x) def matmul(a, b): """jnp.matmul defaults to bfloat16, but this helper function doesn't.""" return jnp.matmul(a, b, precision=jax.lax.Precision.HIGHEST) def unstack(x, axis=0): return tuple( jnp.squeeze(z, axis=axis) for z in jnp.split(x, x.shape[axis], axis=axis) ) @jax.custom_jvp def plus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, tiny_val, jnp.nextafter(jnp.float32(x), jnp.inf) ) @jax.custom_jvp def minus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, -tiny_val, jnp.nextafter(jnp.float32(x), -jnp.inf) ) @plus_eps.defjvp def plus_eps_jvp(primals, tangents): """Make plus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return plus_eps(*primals), tangents[0] @minus_eps.defjvp def minus_eps_jvp(primals, tangents): """Make minus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return minus_eps(*primals), tangents[0] @jax.custom_jvp def expm1(x): """jnp.expm1() has inaccurate gradients when x << 0, this doesn't.""" return jnp.expm1(x) @expm1.defjvp def expm1_jvp(primals, tangents): return expm1(*primals), tangents[0] * jnp.exp(primals[0]) def safe_trig_helper(x, fn, t=100 * jnp.pi): """Helper function used by safe_cos/safe_sin: mods x before sin()/cos().""" return fn(jnp.nan_to_num(jnp.where(jnp.abs(x) < t, x, x % t))) def safe_cos(x): """jnp.cos() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.cos) def safe_sin(x): """jnp.sin() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.sin) @jax.custom_vjp def safe_arctan2(x1, x2): return safe_arctan2_fwd(x1, x2)[0] def safe_arctan2_fwd(x1, x2): return jnp.arctan2(x1, x2), (x1, x2) def safe_arctan2_bwd(res, g): x1, x2 = res denom = remove_zero(x1**2 + x2**2) d1 = g * (x2 / denom) d2 = g * (-x1 / denom) return d1, d2 safe_arctan2.defvjp(safe_arctan2_fwd, safe_arctan2_bwd) def generate_clip_nograd_fn(a_min, a_max): """Generates a function that clips to [a_min, a_max] with no grad effects.""" @jax.custom_jvp def clip_nograd(a): """Clamps `a` from above and below.""" return jnp.clip(a, a_min, a_max) @clip_nograd.defjvp def clip_nograd_jvp(primals, tangents): """Override clips()'s gradient to be a no-op.""" return clip_nograd(primals[0]), tangents[0] return clip_nograd clip_finite_nograd = generate_clip_nograd_fn(min_val, max_val) clip_pos_finite_nograd = generate_clip_nograd_fn(tiny_val, max_val) def clip_pos(x): """Clamps `x` from below to be positive.""" return jnp.maximum(tiny_val, x) def safe_sign(x): """jnp.sign(x) except x=0 is assumed to have a sign of +1, not 0.""" return jnp.where(x < 0, -1, +1) def remove_zero(x): """Shifts `x` away from 0.""" return jnp.where(jnp.abs(x) < tiny_val, tiny_val, x) def clip_finite(x): return jnp.clip(x, min_val, max_val) @jax.custom_vjp def safe_div(n, d): """Divide `n` by `d` but the value and gradient never nan out.""" return safe_div_fwd(n, d)[0] def safe_div_fwd(n, d): r = jnp.clip(n / remove_zero(d), min_val, max_val) return jnp.where(jnp.abs(d) < tiny_val, 0, r), (d, r) def safe_div_bwd(res, g): d, r = res dn = jnp.clip(g / remove_zero(d), min_val, max_val) dd = jnp.clip(-g * r / remove_zero(d), min_val, max_val) return dn, dd safe_div.defvjp(safe_div_fwd, safe_div_bwd) def generate_safe_fn(fn, grad_fn, x_range): """Generate's a `safe` fn() where inputs are clipped in fwd and bwd passes.""" @jax.custom_jvp def safe_fn(x): """fn() with clipped inputs.""" return fn(jnp.clip(x, *x_range)) @safe_fn.defjvp def safe_fn_jvp(primals, tangents): """Backpropagate using the gradient and clipped inputs.""" (x,) = primals (x_dot,) = tangents y = safe_fn(x) y_dot = grad_fn(jnp.clip(x, *x_range), y, x_dot) return y, y_dot return safe_fn # These safe_* functions need to be wrapped in no-op function definitions for # gin to recognize them, otherwise they could just be calls to generate_safe_fn. def safe_log(x): return generate_safe_fn( jnp.log, lambda x, _, x_dot: x_dot / x, (tiny_val, max_val), )(x) def safe_exp(x): return generate_safe_fn( jnp.exp, lambda _, y, x_dot: y * x_dot, (min_val, np.nextafter(np.log(max_val), np.float32(0))), )(x) def safe_sqrt(x): return generate_safe_fn( jnp.sqrt, lambda x, _, x_dot: 0.5 * x_dot / jnp.sqrt(jnp.maximum(tiny_val, x)), (0, max_val), )(x) def safe_log1p(x): return generate_safe_fn( jnp.log1p, lambda x, _, x_dot: x_dot / (1 + x), (np.nextafter(np.float32(-1), np.float32(0)), max_val), )(x) def safe_expm1(x): return generate_safe_fn( expm1, # Note that we wrap around our more accurate expm1. lambda x, _, x_dot: jnp.exp(x) * x_dot, (min_val, np.nextafter(np.log1p(max_val), np.float32(0))), )(x) def safe_arccos(x): """jnp.arccos(x) where x is clipped to [-1, 1].""" y = jnp.arccos(jnp.clip(x, plus_eps(-1), minus_eps(1))) return jnp.where(x >= 1, 0, jnp.where(x <= -1, jnp.pi, y)) def apply_fn_to_grad(grad_fn): """Applies a scalar `grad_fn` function to the gradient of the input.""" @jax.custom_vjp def fn_out(x): return x fn_out.defvjp(lambda x: (x, None), lambda _, y: (grad_fn(y),)) return fn_out def select(cond_pairs, default): """A helpful wrapper around jnp.select() that is easier to read.""" return jnp.select(*zip(*cond_pairs), default) def power_ladder_max_output(p): """The limit of power_ladder(x, p) as x goes to infinity.""" return select( [ (p == -jnp.inf, 1), (p >= 0, jnp.inf), ], safe_div(p - 1, p), ) def power_ladder(x, p, premult=None, postmult=None): """Tukey's power ladder, with a +1 on x, some scaling, and special cases.""" # Compute sign(x) * |p - 1|/p * ((|x|/|p-1| + 1)^p - 1) if premult is not None: x = x * premult xp = jnp.abs(x) xs = xp / jnp.maximum(tiny_val, jnp.abs(p - 1)) p_safe = clip_finite_nograd(remove_zero(p)) y = safe_sign(x) * select( [ (p == 1, xp), (p == 0, safe_log1p(xp)), (p == -jnp.inf, -safe_expm1(-xp)), (p == jnp.inf, safe_expm1(xp)), ], clip_finite_nograd( jnp.abs(p_safe - 1) / p_safe * ((xs + 1) ** p_safe - 1) ), ) if postmult is not None: y = y * postmult return y def inv_power_ladder(y, p, premult=None, postmult=None): """The inverse of `power_ladder()`.""" if postmult is not None: y /= postmult yp = jnp.abs(y) p_safe = clip_finite_nograd(remove_zero(p)) y_max = minus_eps(power_ladder_max_output(p)) yp = override_gradient(jnp.clip(yp, -y_max, y_max), yp) # Clip val, not grad. x = safe_sign(y) * select( [ (p == 1, yp), (p == 0, safe_expm1(yp)), (p == -jnp.inf, -safe_log1p(-yp)), (p == jnp.inf, safe_log1p(yp)), ], jnp.abs(p_safe - 1) * ( ((safe_div(p_safe, jnp.abs(p_safe - 1)) * yp + 1)) ** (1 / p_safe) - 1 ), ) if premult is not None: x /= premult return x def log_lerp(t, v0, v1): """Interpolate log-linearly from `v0` (t=0) to `v1` (t=1).""" if v0 <= 0 or v1 <= 0: raise ValueError(f'Interpolants {v0} and {v1} must be positive.') lv0 = jnp.log(v0) lv1 = jnp.log(v1) return jnp.exp(jnp.clip(t, 0, 1) * (lv1 - lv0) + lv0) def approx_erf(x): """An approximation of erf() that is accurate to within 0.007.""" return jnp.sign(x) * jnp.sqrt(1 - jnp.exp(-(4 / jnp.pi) * x**2)) def create_learning_rate_decay(**kwargs): """A partial evaluation of learning rate decay that can be used with gin.""" return functools.partial(learning_rate_decay, **kwargs) def learning_rate_decay( step, lr_init, lr_final, max_steps, lr_delay_steps=0, lr_delay_mult=1 ): """Continuous learning rate decay function. The returned rate is lr_init when step=0 and lr_final when step=max_steps, and is log-linearly interpolated elsewhere (equivalent to exponential decay). If lr_delay_steps>0 then the learning rate will be scaled by some smooth function of lr_delay_mult, such that the initial learning rate is lr_init*lr_delay_mult at the beginning of optimization but will be eased back to the normal learning rate when steps>lr_delay_steps. Args: step: int, the current optimization step. lr_init: float, the initial learning rate. lr_final: float, the final learning rate. max_steps: int, the number of steps during optimization. lr_delay_steps: int, the number of steps to delay the full learning rate. lr_delay_mult: float, the multiplier on the rate when delaying it. Returns: lr: the learning for current step 'step'. """ if lr_delay_steps > 0: # A kind of reverse cosine decay. delay_rate = lr_delay_mult + (1 - lr_delay_mult) * jnp.sin( 0.5 * jnp.pi * jnp.clip(step / lr_delay_steps, 0, 1) ) else: delay_rate = 1.0 return delay_rate * log_lerp(step / max_steps, lr_init, lr_final) def sorted_lookup(x, xp, fps, device_is_tpu): """Lookup `x` into locations `xp` , return indices and each `[fp]` value.""" if not isinstance(fps, tuple): raise ValueError(f'Input `fps` must be a tuple, but is {type(fps)}.') if<fim_suffix><fim_middle> device_is_tpu: # Identify the location in `xp` that corresponds to each `x`. # The final `True` index in `mask` is the start of the matching interval. mask = x[Ellipsis, None, :] >= xp[Ellipsis, :, None] def find_interval(x): # Grab the value where `mask` switches from True to False, and vice versa. # This approach takes advantage of the fact that `x` is sorted. x0 = jnp.max(jnp.where(mask, x[Ellipsis, None], x[Ellipsis, :1, None]), -2) x1 = jnp.min(jnp.where(~mask, x[Ellipsis, None], x[Ellipsis, -1:, None]), -2) return x0, x1 idx0, idx1 = find_interval(jnp.arange(xp.shape[-1])) vals = [find_interval(fp) for fp in fps] else: # jnp.searchsorted() has slightly different conventions for boundary # handling than the rest of this codebase. idx = jnp.vectorize( lambda a, v: jnp.searchsorted(a, v, side='right'), signature='(n),(m)->(m)', )(xp, x) idx1 = jnp.minimum(idx, xp.shape[-1] - 1) idx0 = jnp.maximum(idx - 1, 0) vals = [] for fp in fps: fp0 = jnp.take_along_axis(fp, idx0, axis=-1) fp1 = jnp.take_along_axis(fp, idx1, axis=-1) vals.append((fp0, fp1))
device_is_tpu: # Identify the location in `xp` that corresponds to each `x`. # The final `True` index in `mask` is the start of the matching interval. mask = x[Ellipsis, None, :] >= xp[Ellipsis, :, None] def find_interval(x): # Grab the value where `mask` switches from True to False, and vice versa. # This approach takes advantage of the fact that `x` is sorted. x0 = jnp.max(jnp.where(mask, x[Ellipsis, None], x[Ellipsis, :1, None]), -2) x1 = jnp.min(jnp.where(~mask, x[Ellipsis, None], x[Ellipsis, -1:, None]), -2) return x0, x1 idx0, idx1 = find_interval(jnp.arange(xp.shape[-1])) vals = [find_interval(fp) for fp in fps] else: # jnp.searchsorted() has slightly different conventions for boundary # handling than the rest of this codebase. idx = jnp.vectorize( lambda a, v: jnp.searchsorted(a, v, side='right'), signature='(n),(m)->(m)', )(xp, x) idx1 = jnp.minimum(idx, xp.shape[-1] - 1) idx0 = jnp.maximum(idx - 1, 0) vals = [] for fp in fps: fp0 = jnp.take_along_axis(fp, idx0, axis=-1) fp1 = jnp.take_along_axis(fp, idx1, axis=-1) vals.append((fp0, fp1))
IF
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/stepfun.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating step functions (piecewise-constant 1D functions). We have a shared naming and dimension convention for these functions. All input/output step functions are assumed to be aligned along the last axis. `t` always indicates the x coordinates of the *endpoints* of a step function. `y` indicates unconstrained values for the *bins* of a step function `w` indicates bin weights that sum to <= 1. `p` indicates non-negative bin values that *integrate* to <= 1. """ from internal import linspline from internal import math from internal import utils import jax import jax.numpy as jnp import numpy as np def query(tq, t, y, left=None, right=None): """Query step function (t, y) at locations tq. Edges repeat by default.""" utils.assert_valid_stepfun(t, y) # Query the step function to recover the interval value. (i0, i1), ((yq, _),) = math.sorted_lookup(tq, t, (y,), utils.device_is_tpu()) # Apply boundary conditions. left = y[Ellipsis, :1] if left is None else left right = y[Ellipsis, -1:] if right is None else right yq = math.select([(i1 == 0, left), (i0 == y.shape[-1], right)], yq) return yq def weight_to_pdf(t, w): """Turn a vector of weights that sums to 1 into a PDF that integrates to 1.""" utils.assert_valid_stepfun(t, w) td = jnp.diff(t) return jnp.where(td < np.finfo(np.float32).tiny, 0, math.safe_div(w, td)) def pdf_to_weight(t, p): """Turn a PDF that integrates to 1 into a vector of weights that sums to 1.""" utils.assert_valid_stepfun(t, p) return p * jnp.diff(t) def integrate_weights(w): """Compute the cumulative sum of w, assuming all weight vectors sum to 1. The output's size on the last dimension is one greater than that of the input, because we're computing the integral corresponding to the endpoints of a step function, not the integral of the interior/bin values. Args: w: Tensor, which will be integrated along the last axis. This is assumed to sum to 1 along the last axis, and this function will (silently) break if that is not the case. Returns: cw0: Tensor, the integral of w, where cw0[..., 0] = 0 and cw0[..., -1] = 1 """ cw = jnp.minimum(1, jnp.cumsum(w[Ellipsis, :-1], axis=-1)) shape = cw.shape[:-1] + (1,) # Ensure that the CDF starts with exactly 0 and ends with exactly 1. cw0 = jnp.concatenate([jnp.zeros(shape), cw, jnp.ones(shape)], axis=-1) return cw0 def invert_cdf(u, t, w_logits): """Invert the CDF defined by (t, w) at the points specified by u in [0, 1).""" utils.assert_valid_stepfun(t, w_logits) # Compute the PDF and CDF for each weight vector. w = jax.nn.softmax(w_logits, axis=-1) cw = integrate_weights(w) # Interpolate into the inverse CDF. t_new = math.sorted_interp(u, cw, t, utils.device_is_tpu()) return t_new def sample( rng, t, w_logits, num_samples, single_jitter=False, deterministic_center=False, eps=jnp.finfo(jnp.float32).eps, ): """Piecewise-Constant PDF sampling from a step function. Args: rng: random number generator (or None for `linspace` sampling). t: [..., num_bins + 1], bin endpoint coordinates (must be sorted) w_logits: [..., num_bins], logits corresponding to bin weights num_samples: int, the number of samples. single_jitter: bool, if True, jitter every sample along each ray by the same amount in the inverse CDF. Otherwise, jitter each sample independently. deterministic_center: bool, if False, when `rng` is None return samples that linspace the entire PDF. If True, skip the front and back of the linspace so that the centers of each PDF interval are returned. eps: float, something like numerical epsilon. Returns: t_samples: jnp.ndarray(float32), [batch_size, num_samples]. """ utils.assert_valid_stepfun(t, w_logits) # Draw uniform samples. if rng is None: # Match the behavior of jax.random.uniform() by spanning [0, 1-eps]. if deterministic_center: pad = 1 / (2 * num_samples) u = jnp.linspace(pad, 1.0 - pad - eps, num_samples) else: u = jnp.linspace(0, 1.0 - eps, num_samples) u = jnp.broadcast_to(u, t.shape[:-1] + (num_samples,)) else: # `u` is in [0, 1) --- it can be zero, but it can never be 1. u_max = eps + (1 - eps) / num_samples max_jitter = (1 - u_max) / (num_samples - 1) - eps d = 1 if single_jitter else num_samples u = jnp.linspace(0, 1 - u_max, num_samples) + jax.random.uniform( rng, t.shape[:-1] + (d,), maxval=max_jitter ) return invert_cdf(u, t, w_logits) def sample_intervals( rng, t, w_logits, num_samples, single_jitter=False, domain=(-jnp.inf, jnp.inf), ): """Sample *intervals* (rather than points) from a step function. Args: rng: random number generator (or None for `linspace` sampling). t: [..., num_bins + 1], bin endpoint coordinates (must be sorted) w_logits: [..., num_bins], logits corresponding to bin weights num_samples: int, the number of intervals to sample. single_jitter: bool, if True, jitter every sample along each ray by the same amount in the inverse CDF. Otherwise, jitter each sample independently. domain: (minval, maxval), the range of valid values for `t`. Returns: t_samples: jnp.ndarray(float32), [batch_size, num_samples]. """ utils.assert_valid_stepfun(t, w_logits) if num_samples <= 1: raise ValueError(f'num_samples must be > 1, is {num_samples}.') # Sample a set of points from the step function. centers = sample( rng, t, w_logits, num_samples, single_jitter, deterministic_center=True ) # The intervals we return will span the midpoints of each adjacent sample. mid = (centers[Ellipsis, 1:] + centers[Ellipsis, :-1]) / 2 # Each first/last fencepost is the reflection of the first/last midpoint # around the first/last sampled center. first = 2 * centers[Ellipsis, :1] - mid[Ellipsis, :1] last = 2 * centers[Ellipsis, -1:] - mid[Ellipsis, -1:] samples = jnp.concatenate([first, mid, last], axis=-1) # We clamp to the limits of the input domain, provided by the caller. samples = jnp.clip(samples, *domain) return samples def lossfun_distortion(t, w): """Compute iint w[i] w[j] |t[i] - t[j]| di dj.""" utils.assert_valid_stepfun(t, w) # The loss incurred between all pairs of intervals. ut = (t[Ellipsis, 1:] + t[Ellipsis, :-1]) / 2 dut = jnp.abs(ut[Ellipsis, :, None] - ut[Ellipsis, None, :]) loss_inter = jnp.sum(w * jnp.sum(w[Ellipsis, None, :] * dut, axis=-1), axis=-1) # The loss incurred within each individual interval with itself. loss_intra = jnp.sum(w**2 * jnp.diff(t), axis=-1) / 3 return loss_inter + loss_intra def weighted_percentile(t, w, ps): """Compute the weighted percentiles of a step function. w's must sum to 1.""" utils.assert_valid_stepfun(t, w) cw = integrate_weights(w) # We want to interpolate into the integrated weights according to `ps`. wprctile = jnp.vectorize(jnp.interp, signature='(n),(m),(m)->(n)')( jnp.array(ps) / 100, cw, t ) return wprctile def resample(t, tp, vp, use_avg=False): """Resample a step function defined by (tp, vp) into intervals t. Notation roughly matches jnp.interp. Resamples by summation by default. Args: t: tensor with shape (..., n+1), the endpoints to resample into. tp: tensor with shape (..., m+1), the endpoints of the step function being resampled. vp: tensor with shape (..., m), the values of the step function being resampled. use_avg: bool, if False, return the sum of the step function for each interval in `t`. If True, return the average, weighted by the width of each interval in `t`. Returns: v: tensor with shape (..., n), the values of the resampled step function. """ utils.assert_valid_stepfun(tp, vp) if use_avg: wp = jnp.diff(tp) v_numer = resample(t, tp, vp * wp, use_avg=False) v_denom = resample(t, tp, wp, use_avg=False) v = math.safe_div(v_numer, v_denom) return v acc = jnp.cumsum(vp, axis=-1) acc0 = jnp.concatenate([jnp.zeros(acc.shape[:-1] + (1,)), acc], axis=-1) acc0_resampled = jnp.vectorize(jnp.interp, signature='(n),(m),(m)->(n)')( t, tp, acc0 ) v = jnp.diff(acc0_resampled, axis=-1) return v def blur_and_resample_weights(tq, t, w, blur_halfwidth): """Blur the (t, w) histogram by blur_halfwidth, then resample it into tq.""" utils.assert_valid_stepfun(t, w) # Convert the histogram to a PDF. p = weight_to_pdf(t, w) # Blur the PDF step function into a piecewise linear spline PDF. t_linspline, p_linspline = linspline.blur_stepfun(t, p, blur_halfwidth) # Integrate the spline PDF, then query it to get integrated weights. quad = linspline.compute_integral(t_linspline, p_linspline) acc_wq = linspline.interpolate_integral(tq, t_linspline, *quad) # Undo the integration to get weights. wq<fim_suffix><fim_middle> = jnp.diff(acc_wq, axis=-1)
= jnp.diff(acc_wq, axis=-1)
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/geopoly.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for constructing geodesic polyhedron, which are used as a basis.""" import itertools import numpy as np def compute_sq_dist(mat0, mat1=None): """Compute the squared Euclidean distance between all pairs of columns.""" if mat1 is None: mat1 = mat0 # Use the fact that ||x - y||^2 == ||x||^2 + ||y||^2 - 2 x^T y. sq_norm0 = np.sum(mat0**2, 0) sq_norm1 = np.sum(mat1**2, 0) sq_dist = sq_norm0[:, None] + sq_norm1[None, :] - 2 * mat0.T @ mat1 sq_dist = np.maximum(0, sq_dist) # Negative values must be numerical errors. return sq_dist def compute_tesselation_weights(v): """Tesselate the vertices of a triangle by a factor of `v`.""" if v < 1: raise ValueError(f'v {v} must be >= 1') int_weights = [] for i in range(v + 1): for j in range(v + 1 - i): int_weights.append((i, j, v - (i + j))) int_weights = np.array(int_weights) weights = int_weights / v # Barycentric weights. return weights def tesselate_geodesic(base_verts, base_faces, v, eps=1e-4): """Tesselate the vertices of a geodesic polyhedron. Args: base_verts: tensor of floats, the vertex coordinates of the geodesic. base_faces: tensor of ints, the indices of the vertices of base_verts that constitute eachface of the polyhedra. v: int, the factor of the tesselation (v==1 is a no-op). eps: float, a small value used to determine if two vertices are the same. Returns: verts: a tensor of floats, the coordinates of the tesselated vertices. """ if not isinstance(v, int): raise ValueError(f'v {v} must an integer') tri_weights = compute_tesselation_weights(v) verts = [] for base_face in base_faces: new_verts = np.matmul(tri_weights, base_verts[base_face, :]) new_verts<fim_suffix><fim_middle> /= np.sqrt(np.sum(new_verts**2, 1, keepdims=True))
/= np.sqrt(np.sum(new_verts**2, 1, keepdims=True))
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/stepfun.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating step functions (piecewise-constant 1D functions). We have a shared naming and dimension convention for these functions. All input/output step functions are assumed to be aligned along the last axis. `t` always indicates the x coordinates of the *endpoints* of a step function. `y` indicates unconstrained values for the *bins* of a step function `w` indicates bin weights that sum to <= 1. `p` indicates non-negative bin values that *integrate* to <= 1. """ from internal import linspline from internal import math from internal import utils import jax import jax.numpy as jnp import numpy as np def query(tq, t, y, left=None, right=None): """Query step function (t, y) at locations tq. Edges repeat by default.""" utils.assert_valid_stepfun(t, y) # Query the step function to recover the interval value. (i0, i1), ((yq, _),) = math.sorted_lookup(tq, t, (y,), utils.device_is_tpu()) # Apply boundary conditions. left = y[Ellipsis, :1] if left is None else left right = y[Ellipsis, -1:] if right is None else right yq = math.select([(i1 == 0, left), (i0 == y.shape[-1], right)], yq) return yq def weight_to_pdf(t, w): """Turn a vector of weights that sums to 1 into a PDF that integrates to 1.""" utils.assert_valid_stepfun(t, w) td = jnp.diff(t) return jnp.where(td < np.finfo(np.float32).tiny, 0, math.safe_div(w, td)) def pdf_to_weight(t, p): """Turn a PDF that integrates to 1 into a vector of weights that sums to 1.""" utils.assert_valid_stepfun(t, p) return p * jnp.diff(t) def integrate_weights(w): """Compute the cumulative sum of w, assuming all weight vectors sum to 1. The output's size on the last dimension is one greater than that of the input, because we're computing the integral corresponding to the endpoints of a step function, not the integral of the interior/bin values. Args: w: Tensor, which will be integrated along the last axis. This is assumed to sum to 1 along the last axis, and this function will (silently) break if that is not the case. Returns: cw0: Tensor, the integral of w, where cw0[..., 0] = 0 and cw0[..., -1] = 1 """ cw = jnp.minimum(1, jnp.cumsum(w[Ellipsis, :-1], axis=-1)) shape = cw.shape[:-1] + (1,) # Ensure that the CDF starts with exactly 0 and ends with exactly 1. cw0 = jnp.concatenate([jnp.zeros(shape), cw, jnp.ones(shape)], axis=-1) return cw0 def invert_cdf(u, t, w_logits): """Invert the CDF defined by (t, w) at the points specified by u in [0, 1).""" utils.assert_valid_stepfun(t, w_logits) # Compute the PDF and CDF for each weight vector. w = jax.nn.softmax(w_logits, axis=-1) cw = integrate_weights(w) # Interpolate into the inverse CDF. t_new = math.sorted_interp(u, cw, t, utils.device_is_tpu()) return<fim_suffix><fim_middle> t_new
t_new
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/linspline.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Helper functions for linear splines.""" import functools from internal import math from internal import utils import jax from jax.experimental import checkify import jax.numpy as jnp def check_zero_endpoints(y): checkify.check(jnp.all(y[Ellipsis, 0] == 0), 'Splines must all start with 0.') checkify.check(jnp.all(y[Ellipsis, -1] == 0), 'Splines must all end with 0.') def query(tq, t, v): """Query linear spline (t, v) at tq.""" utils.assert_valid_linspline(t, v) interp = functools.partial(jnp.interp, left=0, right=0) return jnp.vectorize(interp, signature='(n),(m),(m)->(n)')(tq, t, v) def integrate(t, w): """Integrate (t, w) according to the trapezoid rule.""" utils.assert_valid_linspline(t, w) return 0.5 * jnp.sum((w[Ellipsis, :-1] + w[Ellipsis, 1:]) * jnp.diff(t), axis=-1) def normalize(t, w, eps=jnp.finfo(jnp.float32).eps ** 2): """Make w integrate to 1.""" utils.assert_valid_linspline(t, w) return w / jnp.maximum(eps, integrate(t, w))[Ellipsis, None] def insert_knot(ti, t, y): """Inserts knots ti into the linear spline (t, w). Assumes zero endpoints.""" utils.assert_valid_linspline(t, y) check_zero_endpoints(y) # Compute the spline value at the insertion points. yi = query(ti, t, y) # Concatenate the insertion points and values onto the end of each spline. ti_ex = jnp.broadcast_to(ti, t.shape[: -len(ti.shape)] + ti.shape) yi_ex = jnp.broadcast_to(yi, y.shape[: -len(yi.shape)] + yi.shape) to = jnp.concatenate([t, ti_ex], axis=-1) yo = jnp.concatenate([y, yi_ex], axis=-1) # Sort the spline according to t. sort_idx = jnp.argsort(to) to = jnp.take_along_axis(to, sort_idx, axis=-1) yo = jnp.take_along_axis(yo, sort_idx, axis=-1) return to, yo def clamp(t, y, minval, maxval): """Clamp (t, y) to be zero outside of t in [minval, maxval].""" utils.assert_valid_linspline(t, y) check_zero_endpoints(y) # Add in extra points at and immediately above/below the min/max vals. ti = jnp.concatenate( [ math.minus_eps(minval), minval, maxval, math.plus_eps(maxval), ], axis=-1, ) tc, yo = insert_knot(ti, t, y) # Zero the spline values outside of [minval, maxval]. yc = jnp.where(tc > maxval, 0, jnp.where(tc < minval, 0, yo)) return tc, yc def compute_integral(t, y): """Integrate a linear spline into a piecewise quadratic spline.""" utils.assert_valid_linspline(t, y) eps = jnp.finfo(jnp.float32).eps ** 2 dt = jnp.diff(t) a = jnp.diff(y) / jnp.maximum(eps, 2 * dt) b = y[Ellipsis, :-1] # The integral has an ambiguous global offset here, which we set to 0. c1 = 0.5 * jnp.cumsum(dt[Ellipsis, :-1] * (y[Ellipsis, :-2] + y[Ellipsis, 1:-1]), axis=-1) c<fim_suffix><fim_middle> = jnp.concatenate([jnp.zeros_like(y[Ellipsis, :1]), c1], axis=-1)
= jnp.concatenate([jnp.zeros_like(y[Ellipsis, :1]), c1], axis=-1)
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/math.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Mathy utility functions.""" import functools import jax import jax.numpy as jnp import numpy as np tiny_val = np.float32(np.finfo(np.float32).tiny) min_val = np.float32(np.finfo(np.float32).min) max_val = np.float32(np.finfo(np.float32).max) def laplace_cdf(x, beta): alpha = 1 / beta return alpha * (0.5 + 0.5 * safe_sign(x) * (jnp.exp(-jnp.abs(x) / beta) - 1)) def scaled_softplus(x, scale=100.0): return (1.0 / scale) * jax.nn.softplus(scale * x) def matmul(a, b): """jnp.matmul defaults to bfloat16, but this helper function doesn't.""" return jnp.matmul(a, b, precision=jax.lax.Precision.HIGHEST) def unstack(x, axis=0): return tuple( jnp.squeeze(z, axis=axis) for z in jnp.split(x, x.shape[axis], axis=axis) ) @jax.custom_jvp def plus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, tiny_val, jnp.nextafter(jnp.float32(x), jnp.inf) ) @jax.custom_jvp def minus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, -tiny_val, jnp.nextafter(jnp.float32(x), -jnp.inf) ) @plus_eps.defjvp def plus_eps_jvp(primals, tangents): """Make plus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return plus_eps(*primals), tangents[0] @minus_eps.defjvp def minus_eps_jvp(primals, tangents): """Make minus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return minus_eps(*primals), tangents[0] @jax.custom_jvp def expm1(x): """jnp.expm1() has inaccurate gradients when x << 0, this doesn't.""" return jnp.expm1(x) @expm1.defjvp def expm1_jvp(primals, tangents): return expm1(*primals), tangents[0] * jnp.exp(primals[0]) def safe_trig_helper(x, fn, t=100 * jnp.pi): """Helper function used by safe_cos/safe_sin: mods x before sin()/cos().""" return fn(jnp.nan_to_num(jnp.where(jnp.abs(x) < t, x, x % t))) def safe_cos(x): """jnp.cos() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.cos) def safe_sin(x): """jnp.sin() on a TPU may NaN out for large values.""" return<fim_suffix><fim_middle> safe_trig_helper(x, jnp.sin)
safe_trig_helper(x, jnp.sin)
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/coord.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating coordinate spaces and distances along rays.""" from internal import geopoly from internal import math import jax from jax import random import jax.numpy as jnp import numpy as np def contract(x): """Contracts points towards the origin (Eq 10 of arxiv.org/abs/2111.12077).""" # Clamping to 1 produces correct scale inside |x| < 1 x_mag_sq = jnp.maximum(1, jnp.sum(x**2, axis=-1, keepdims=True)) scale = (2 * jnp.sqrt(x_mag_sq) - 1) / x_mag_sq z = scale * x return<fim_suffix><fim_middle> z
z
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/math.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Mathy utility functions.""" import functools import jax import jax.numpy as jnp import numpy as np tiny_val = np.float32(np.finfo(np.float32).tiny) min_val = np.float32(np.finfo(np.float32).min) max_val = np.float32(np.finfo(np.float32).max) def laplace_cdf(x, beta): alpha = 1 / beta return alpha * (0.5 + 0.5 * safe_sign(x) * (jnp.exp(-jnp.abs(x) / beta) - 1)) def scaled_softplus(x, scale=100.0): return (1.0 / scale) * jax.nn.softplus(scale * x) def matmul(a, b): """jnp.matmul defaults to bfloat16, but this helper function doesn't.""" return jnp.matmul(a, b, precision=jax.lax.Precision.HIGHEST) def unstack(x, axis=0): return tuple( jnp.squeeze(z, axis=axis) for z in jnp.split(x, x.shape[axis], axis=axis) ) @jax.custom_jvp def plus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, tiny_val, jnp.nextafter(jnp.float32(x), jnp.inf) ) @jax.custom_jvp def minus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, -tiny_val, jnp.nextafter(jnp.float32(x), -jnp.inf) ) @plus_eps.defjvp def plus_eps_jvp(primals, tangents): """Make plus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return plus_eps(*primals), tangents[0] @minus_eps.defjvp def minus_eps_jvp(primals, tangents): """Make minus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return minus_eps(*primals), tangents[0] @jax.custom_jvp def expm1(x): """jnp.expm1() has inaccurate gradients when x << 0, this doesn't.""" return jnp.expm1(x) @expm1.defjvp def expm1_jvp(primals, tangents): return expm1(*primals), tangents[0] * jnp.exp(primals[0]) def safe_trig_helper(x, fn, t=100 * jnp.pi): """Helper function used by safe_cos/safe_sin: mods x before sin()/cos().""" return fn(jnp.nan_to_num(jnp.where(jnp.abs(x) < t, x, x % t))) def safe_cos(x): """jnp.cos() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.cos) def safe_sin(x): """jnp.sin() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.sin) @jax.custom_vjp def safe_arctan2(x1, x2): return safe_arctan2_fwd(x1, x2)[0] def safe_arctan2_fwd(x1, x2): return jnp.arctan2(x1, x2), (x1, x2) def safe_arctan2_bwd(res, g): x1, x2 = res denom = remove_zero(x1**2 + x2**2) d1 = g * (x2 / denom) d2 = g * (-x1 / denom) return d1, d2 safe_arctan2.defvjp(safe_arctan2_fwd, safe_arctan2_bwd) def generate_clip_nograd_fn(a_min, a_max): """Generates a function that clips to [a_min, a_max] with no grad effects.""" @jax.custom_jvp def clip_nograd(a): """Clamps `a` from above and below.""" return jnp.clip(a, a_min, a_max) @clip_nograd.defjvp def clip_nograd_jvp(primals, tangents): """Override clips()'s gradient to be a no-op.""" return clip_nograd(primals[0]), tangents[0] return clip_nograd clip_finite_nograd = generate_clip_nograd_fn(min_val, max_val) clip_pos_finite_nograd = generate_clip_nograd_fn(tiny_val, max_val) def clip_pos(x): """Clamps `x` from below to be positive.""" return jnp.maximum(tiny_val, x) def safe_sign(x): """jnp.sign(x) except x=0 is assumed to have a sign of +1, not 0.""" return jnp.where(x < 0, -1, +1) def remove_zero(x): """Shifts `x` away from 0.""" return jnp.where(jnp.abs(x) < tiny_val, tiny_val, x) def clip_finite(x): return jnp.clip(x, min_val, max_val) @jax.custom_vjp def safe_div(n, d): """Divide `n` by `d` but the value and gradient never nan out.""" return safe_div_fwd(n, d)[0] def safe_div_fwd(n, d): r = jnp.clip(n / remove_zero(d), min_val, max_val) return jnp.where(jnp.abs(d) < tiny_val, 0, r), (d, r) def safe_div_bwd(res, g): d, r = res dn = jnp.clip(g / remove_zero(d), min_val, max_val) dd = jnp.clip(-g * r / remove_zero(d), min_val, max_val) return dn, dd safe_div.defvjp(safe_div_fwd, safe_div_bwd) def generate_safe_fn(fn, grad_fn, x_range): """Generate's a `safe` fn() where inputs are clipped in fwd and bwd passes.""" @jax.custom_jvp def safe_fn(x): """fn() with clipped inputs.""" return fn(jnp.clip(x, *x_range)) @safe_fn.defjvp def safe_fn_jvp(primals, tangents): """Backpropagate using the gradient and clipped inputs.""" (x,) = primals (x_dot,) = tangents y = safe_fn(x) y_dot = grad_fn(jnp.clip(x, *x_range), y, x_dot) return y, y_dot return safe_fn # These safe_* functions need to be wrapped in no-op function definitions for # gin to recognize them, otherwise they could just be calls to generate_safe_fn. def safe_log(x): return generate_safe_fn( jnp.log, lambda x, _, x_dot: x_dot / x, (tiny_val, max_val), )(x) def safe_exp(x): return generate_safe_fn( jnp.exp, lambda _, y, x_dot: y * x_dot, (min_val, np.nextafter(np.log(max_val), np.float32(0))), )(x) def safe_sqrt(x): return generate_safe_fn( jnp.sqrt, lambda x, _, x_dot: 0.5 * x_dot / jnp.sqrt(jnp.maximum(tiny_val, x)), (0, max_val), )(x) def safe_log1p(x): return generate_safe_fn( jnp.log1p, lambda x, _, x_dot: x_dot / (1 + x), (np.nextafter(np.float32(-1), np.float32(0)), max_val), )(x) def safe_expm1(x): return generate_safe_fn( expm1, # Note that we wrap around our more accurate expm1. lambda x, _, x_dot: jnp.exp(x) * x_dot, (min_val, np.nextafter(np.log1p(max_val), np.float32(0))), )(x) def safe_arccos(x): """jnp.arccos(x) where x is clipped to [-1, 1].""" y = jnp.arccos(jnp.clip(x, plus_eps(-1), minus_eps(1))) return jnp.where(x >= 1, 0, jnp.where(x <= -1, jnp.pi, y)) def apply_fn_to_grad(grad_fn): """Applies a scalar `grad_fn` function to the gradient of the input.""" @jax.custom_vjp def fn_out(x): return x fn_out.defvjp(lambda x: (x, None), lambda _, y: (grad_fn(y),)) return fn_out def select(cond_pairs, default): """A helpful wrapper around jnp.select() that is easier to read.""" return jnp.select(*zip(*cond_pairs), default) def power_ladder_max_output(p): """The limit of power_ladder(x, p) as x goes to infinity.""" return select( [ (p == -jnp.inf, 1), (p >= 0, jnp.inf), ], safe_div(p - 1, p), ) def power_ladder(x, p, premult=None, postmult=None): """Tukey's power ladder, with a +1 on x, some scaling, and special cases.""" # Compute sign(x) * |p - 1|/p * ((|x|/|p-1| + 1)^p - 1) if premult is not None: x = x * premult xp = jnp.abs(x) xs = xp / jnp.maximum(tiny_val, jnp.abs(p - 1)) p_safe = clip_finite_nograd(remove_zero(p)) y = safe_sign(x) * select( [ (p == 1, xp), (p == 0, safe_log1p(xp)), (p == -jnp.inf, -safe_expm1(-xp)), (p == jnp.inf, safe_expm1(xp)), ], clip_finite_nograd( jnp.abs(p_safe - 1) / p_safe * ((xs + 1) ** p_safe - 1) ), ) if postmult is not None: y = y * postmult return y def inv_power_ladder(y, p, premult=None, postmult=None): """The inverse of `power_ladder()`.""" if postmult is not None: y /= postmult yp = jnp.abs(y) p_safe = clip_finite_nograd(remove_zero(p)) y_max = minus_eps(power_ladder_max_output(p)) yp = override_gradient(jnp.clip(yp, -y_max, y_max), yp) # Clip val, not grad. x = safe_sign(y) * select( [ (p == 1, yp), (p == 0, safe_expm1(yp)), (p == -jnp.inf, -safe_log1p(-yp)), (p == jnp.inf, safe_log1p(yp)), ], jnp.abs(p_safe - 1) * ( ((safe_div(p_safe, jnp.abs(p_safe - 1)) * yp + 1)) ** (1 / p_safe) - 1 ), ) if premult is not None: x /= premult return x def log_lerp(t, v0, v1): """Interpolate log-linearly from `v0` (t=0) to `v1` (t=1).""" if v0 <= 0 or v1 <= 0: raise ValueError(f'Interpolants {v0} and {v1} must be positive.') lv0 = jnp.log(v0) lv1 = jnp.log(v1) return jnp.exp(jnp.clip(t, 0, 1) * (lv1 - lv0) + lv0) def approx_erf(x): """An approximation of erf() that is accurate to within 0.007.""" return jnp.sign(x) * jnp.sqrt(1 - jnp.exp(-(4 / jnp.pi) * x**2)) def create_learning_rate_decay(**kwargs): """A partial evaluation of learning rate decay that can be used with gin.""" return functools.partial(learning_rate_decay, **kwargs) def learning_rate_decay( step, lr_init, lr_final, max_steps, lr_delay_steps=0, lr_delay_mult=1 ): """Continuous learning rate decay function. The returned rate is lr_init when step=0 and lr_final when step=max_steps, and is log-linearly interpolated elsewhere (equivalent to exponential decay). If lr_delay_steps>0 then the learning rate will be scaled by some smooth function of lr_delay_mult, such that the initial learning rate is lr_init*lr_delay_mult at the beginning of optimization but will be eased back to the normal learning rate when steps>lr_delay_steps. Args: step: int, the current optimization step. lr_init: float, the initial learning rate. lr_final: float, the final learning rate. max_steps: int, the number of steps during optimization. lr_delay_steps: int, the number of steps to delay the full learning rate. lr_delay_mult: float, the multiplier on the rate when delaying it. Returns: lr: the learning for current step 'step'. """ if lr_delay_steps > 0: # A kind of reverse cosine decay. delay_rate = lr_delay_mult + (1 - lr_delay_mult) * jnp.sin( 0.5 * jnp.pi * jnp.clip(step / lr_delay_steps, 0, 1) ) else: delay_rate = 1.0 return delay_rate * log_lerp(step / max_steps, lr_init, lr_final) def sorted_lookup(x, xp, fps, device_is_tpu): """Lookup `x` into locations `xp` , return indices and each `[fp]` value.""" if not isinstance(fps, tuple): raise ValueError(f'Input `fps` must be a tuple, but is {type(fps)}.') if device_is_tpu: # Identify the location in `xp` that corresponds to each `x`. # The final `True` index in `mask` is the start of the matching interval. mask = x[Ellipsis, None, :] >= xp[Ellipsis, :, None] def find_interval(x): # Grab the value where `mask` switches from True to False, and vice versa. # This approach takes advantage of the fact that `x` is sorted. x0 = jnp.max(jnp.where(mask, x[Ellipsis, None], x[Ellipsis, :1, None]), -2) x1 = jnp.min(jnp.where(~mask, x[Ellipsis, None], x[Ellipsis, -1:, None]), -2) return x0, x1 idx0, idx1 = find_interval(jnp.arange(xp.shape[-1])) vals = [find_interval(fp) for fp in fps] else: # jnp.searchsorted() has slightly different conventions for boundary # handling than the rest of this codebase. idx = jnp.vectorize( lambda a, v: jnp.searchsorted(a, v, side='right'), signature='(n),(m)->(m)', )(xp, x) idx1 = jnp.minimum(idx, xp.shape[-1] - 1) idx0 = jnp.maximum(idx - 1, 0) vals<fim_suffix><fim_middle> = []
= []
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/ref_utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Functions for reflection directions and directional encodings.""" import math from internal import math as math_lib import jax.numpy as jnp import numpy as np def reflect(viewdirs, normals): """Reflect view directions about normals. The reflection of a vector v about a unit vector n is a vector u such that dot(v, n) = dot(u, n), and dot(u, u) = dot(v, v). The solution to these two equations is u = 2 dot(n, v) n - v. Args: viewdirs: [..., 3] array of view directions. normals: [..., 3] array of normal directions (assumed to be unit vectors). Returns: [..., 3] array of reflection directions. """ return ( 2.0 * jnp.sum(normals * viewdirs, axis=-1, keepdims=True) * normals - viewdirs ) def l2_normalize(x, grad_eps=jnp.finfo(jnp.float32).eps): """Normalize x to unit length along last axis. Normalizing vectors is surprisingly tricky, because you have to address the case where the denominator in the normalization is tiny or zero, in which case gradients will explode. For this reason, we perform two normalizations: in the forward pass, we clamp the denominator with ~1e-40, but in the backward pass we clamp with `grad_eps`, which defaults to ~1e-7. This guarantees that the output of this function is unit norm (unless x is very very small) while preventing exploding gradients. Args: x: The array of values to normalize. grad_eps: The value to clip the squared norm by before division in the backward pass. Returns: A normalized array x / ||x||, normalized along the last axis. """ tiny = jnp.finfo(jnp.float32).tiny grad_eps = jnp.maximum(tiny, grad_eps) denom_sq = jnp.sum(x**2, axis=-1, keepdims=True) normal_val = x / jnp.sqrt(jnp.maximum(tiny, denom_sq)) normal_grad = x / jnp.sqrt(jnp.maximum(grad_eps, denom_sq)) # Use `normal_val` in the forward pass but `normal_grad` in the backward pass. normal = math_lib.override_gradient(normal_val, normal_grad) return jnp.where(denom_sq < tiny, jnp.zeros_like(normal), normal) def compute_weighted_mae(weights, normals, normals_gt): """Compute weighted mean angular error, assuming normals are unit length.""" angles = math_lib.safe_arccos((normals * normals_gt).sum(axis=-1)) return (180.0 / jnp.pi) * ((weights * angles).sum() / weights.sum()) def generalized_binomial_coeff(a, k): """Compute generalized binomial coefficients.""" return np.prod(a - np.arange(k)) / math.factorial(k) def assoc_legendre_coeff(l, m, k): """Compute associated Legendre polynomial coefficients. Returns the coefficient of the cos^k(theta)*sin^m(theta) term in the (l, m)th associated Legendre polynomial, P_l^m(cos(theta)). Args: l: associated Legendre polynomial degree. m: associated Legendre polynomial order. k: power of cos(theta). Returns: A float, the coefficient of the term corresponding to the inputs. """ return ( (-1) ** m * 2**l * math.factorial(l) / math.factorial(k) / math.factorial(l - k - m) * generalized_binomial_coeff(0.5 * (l + k + m - 1.0), l) ) def sph_harm_coeff(l, m, k): """Compute spherical harmonic coefficients.""" return np.sqrt( (2.0 * l + 1.0) * math.factorial(l - m) / (4.0 * np.pi * math.factorial(l + m)) ) * assoc_legendre_coeff(l, m, k) def get_ml_array(deg_view): """Create a list with all pairs of (l, m) values to use in the encoding.""" ml_list = [] for i in range(deg_view): l = 2**i # Only use nonnegative m values, later splitting real and imaginary parts. for m in range(l + 1): ml_list.append((m, l)) # Convert list into a numpy array. ml_array = np.array(ml_list).T return ml_array def generate_ide_fn(deg_view): """Generate integrated directional encoding (IDE) function. This function returns a function that computes the integrated directional encoding from Equations 6-8 of arxiv.org/abs/2112.03907. Args: deg_view: number of spherical harmonics degrees to use. Returns: A function for evaluating integrated directional encoding. Raises: ValueError: if deg_view is larger than 5. """ if deg_view > 5: raise ValueError('Only deg_view of at most 5 is numerically stable.') ml_array = get_ml_array(deg_view) l_max = 2 ** (deg_view - 1) # Create a matrix corresponding to ml_array holding all coefficients, which, # when multiplied (from the right) by the z coordinate Vandermonde matrix, # results in the z component of the encoding. mat = np.zeros((l_max + 1, ml_array.shape[1])) for i, (m, l) in enumerate(ml_array.T): for k in range(l - m + 1): mat[k, i] = sph_harm_coeff(l, m, k) def integrated_dir_enc_fn(xyz, kappa_inv): """Function returning integrated directional encoding (IDE). Args: xyz: [..., 3] array of Cartesian coordinates of directions to evaluate at. kappa_inv: [..., 1] reciprocal of the concentration parameter of the von Mises-Fisher distribution. Returns: An array with the resulting IDE. """ x = xyz[Ellipsis, 0:1] y = xyz[Ellipsis, 1:2] z = xyz[Ellipsis, 2:3] # Compute z Vandermonde matrix. vmz = jnp.concatenate([z**i for i in range(mat.shape[0])], axis=-1) # Compute x+iy Vandermonde matrix. vmxy = jnp.concatenate([(x + 1j * y) ** m for m in ml_array[0, :]], axis=-1) # Get spherical harmonics. sph_harms = vmxy * math_lib.matmul(vmz, mat) # Apply attenuation function using the von Mises-Fisher distribution # concentration parameter, kappa. sigma = 0.5 * ml_array[1, :] * (ml_array[1, :] + 1) ide<fim_suffix><fim_middle> = sph_harms * jnp.exp(-sigma * kappa_inv)
= sph_harms * jnp.exp(-sigma * kappa_inv)
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/ref_utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Functions for reflection directions and directional encodings.""" import math from internal import math as math_lib import jax.numpy as jnp import numpy as np def reflect(viewdirs, normals): """Reflect view directions about normals. The reflection of a vector v about a unit vector n is a vector u such that dot(v, n) = dot(u, n), and dot(u, u) = dot(v, v). The solution to these two equations is u = 2 dot(n, v) n - v. Args: viewdirs: [..., 3] array of view directions. normals: [..., 3] array of normal directions (assumed to be unit vectors). Returns: [..., 3] array of reflection directions. """ return ( 2.0 * jnp.sum(normals * viewdirs, axis=-1, keepdims=True) * normals - viewdirs ) def l2_normalize(x, grad_eps=jnp.finfo(jnp.float32).eps): """Normalize x to unit length along last axis. Normalizing vectors is surprisingly tricky, because you have to address the case where the denominator in the normalization is tiny or zero, in which case gradients will explode. For this reason, we perform two normalizations: in the forward pass, we clamp the denominator with ~1e-40, but in the backward pass we clamp with `grad_eps`, which defaults to ~1e-7. This guarantees that the output of this function is unit norm (unless x is very very small) while preventing exploding gradients. Args: x: The array of values to normalize. grad_eps: The value to clip the squared norm by before division in the backward pass. Returns: A normalized array x / ||x||, normalized along the last axis. """ tiny = jnp.finfo(jnp.float32).tiny grad_eps = jnp.maximum(tiny, grad_eps) denom_sq = jnp.sum(x**2, axis=-1, keepdims=True) normal_val = x / jnp.sqrt(jnp.maximum(tiny, denom_sq)) normal_grad = x / jnp.sqrt(jnp.maximum(grad_eps, denom_sq)) # Use `normal_val` in the forward pass but `normal_grad` in the backward pass. normal = math_lib.override_gradient(normal_val, normal_grad) return jnp.where(denom_sq < tiny, jnp.zeros_like(normal), normal) def compute_weighted_mae(weights, normals, normals_gt): """Compute weighted mean angular error, assuming normals are unit length.""" angles = math_lib.safe_arccos((normals * normals_gt).sum(axis=-1)) return (180.0 / jnp.pi) * ((weights * angles).sum() / weights.sum()) def generalized_binomial_coeff(a, k): """Compute generalized binomial coefficients.""" return np.prod(a - np.arange(k)) / math.factorial(k) def assoc_legendre_coeff(l, m, k): """Compute associated Legendre polynomial coefficients. Returns the coefficient of the cos^k(theta)*sin^m(theta) term in the (l, m)th associated Legendre polynomial, P_l^m(cos(theta)). Args: l: associated Legendre polynomial degree. m: associated Legendre polynomial order. k: power of cos(theta). Returns: A float, the coefficient of the term corresponding to the inputs. """ return ( (-1) ** m * 2**l * math.factorial(l) / math.factorial(k) / math.factorial(l - k - m) * generalized_binomial_coeff(0.5 * (l + k + m - 1.0), l) ) def sph_harm_coeff(l, m, k): """Compute spherical harmonic coefficients.""" return np.sqrt( (2.0 * l + 1.0) * math.factorial(l - m) / (4.0 * np.pi * math.factorial(l + m)) ) * assoc_legendre_coeff(l, m, k) def get_ml_array(deg_view): """Create a list with all pairs of (l, m) values to use in the encoding.""" ml_list = [] for i in range(deg_view): l = 2**i # Only use nonnegative m values, later splitting real and imaginary parts. for m in range(l + 1): ml_list.append((m, l)) # Convert list into a numpy array. ml_array = np.array(ml_list).T return ml_array def generate_ide_fn(deg_view): """Generate integrated directional encoding (IDE) function. This function returns a function that computes the integrated directional encoding from Equations 6-8 of arxiv.org/abs/2112.03907. Args: deg_view: number of spherical harmonics degrees to use. Returns: A function for evaluating integrated directional encoding. Raises: ValueError: if deg_view is larger than 5. """ if deg_view > 5: raise ValueError('Only deg_view of at most 5 is numerically stable.') ml_array = get_ml_array(deg_view) l_max = 2 ** (deg_view - 1) # Create a matrix corresponding to ml_array holding all coefficients, which, # when multiplied (from the right) by the z coordinate Vandermonde matrix, # results in the z component of the encoding. mat = np.zeros((l_max + 1, ml_array.shape[1])) for i, (m, l) in enumerate(ml_array.T): for k in range(l - m + 1): mat[k, i] = sph_harm_coeff(l, m, k) def integrated_dir_enc_fn(xyz, kappa_inv): """Function returning integrated directional encoding (IDE). Args: xyz: [..., 3] array of Cartesian coordinates of directions to evaluate at. kappa_inv: [..., 1] reciprocal of the concentration parameter of the von Mises-Fisher distribution. Returns: An array with the resulting IDE. """ x = xyz[Ellipsis, 0:1] y = xyz[Ellipsis, 1:2] z = xyz[Ellipsis, 2:3] # Compute z Vandermonde matrix. vmz<fim_suffix><fim_middle> = jnp.concatenate([z**i for i in range(mat.shape[0])], axis=-1)
= jnp.concatenate([z**i for i in range(mat.shape[0])], axis=-1)
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/coord.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating coordinate spaces and distances along rays.""" from internal import geopoly from internal import math import jax from jax import random import jax.numpy as jnp import numpy as np def contract(x): """Contracts points towards the origin (Eq 10 of arxiv.org/abs/2111.12077).""" # Clamping to 1 produces correct scale inside |x| < 1 x_mag_sq = jnp.maximum(1, jnp.sum(x**2, axis=-1, keepdims=True)) scale = (2 * jnp.sqrt(x_mag_sq) - 1) / x_mag_sq z = scale * x return z def inv_contract(z): """The inverse of contract().""" # Clamping to 1 produces correct scale inside |z| < 1 z_mag_sq<fim_suffix><fim_middle> = jnp.maximum(1, jnp.sum(z**2, axis=-1, keepdims=True))
= jnp.maximum(1, jnp.sum(z**2, axis=-1, keepdims=True))
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except queue.Empty: continue logging.info('Got data in %0.3fs', time.time() - get_start) yield next_value #<fim_suffix><fim_middle> Thread exception will be raised here
Thread exception will be raised here
LINE_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/stepfun.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating step functions (piecewise-constant 1D functions). We have a shared naming and dimension convention for these functions. All input/output step functions are assumed to be aligned along the last axis. `t` always indicates the x coordinates of the *endpoints* of a step function. `y` indicates unconstrained values for the *bins* of a step function `w` indicates bin weights that sum to <= 1. `p` indicates non-negative bin values that *integrate* to <= 1. """ from internal import linspline from internal import math from internal import utils import jax import jax.numpy as jnp import numpy as np def query(tq, t, y, left=None, right=None): """Query step function (t, y) at locations tq. Edges repeat by default.""" utils.assert_valid_stepfun(t, y) # Query the step function to recover the interval value. (i0, i1), ((yq, _),) = math.sorted_lookup(tq, t, (y,), utils.device_is_tpu()) # Apply boundary conditions. left = y[Ellipsis, :1] if left is None else left right = y[Ellipsis, -1:] if right is None else right yq = math.select([(i1 == 0, left), (i0 == y.shape[-1], right)], yq) return yq def weight_to_pdf(t, w): """Turn a vector of weights that sums to 1 into a PDF that integrates to 1.""" utils.assert_valid_stepfun(t, w) td = jnp.diff(t) return jnp.where(td < np.finfo(np.float32).tiny, 0, math.safe_div(w, td)) def pdf_to_weight(t, p): """Turn a PDF that integrates to 1 into a vector of weights that sums to 1.""" utils.assert_valid_stepfun(t, p) return p * jnp.diff(t) def integrate_weights(w): """Compute the cumulative sum of w, assuming all weight vectors sum to 1. The output's size on the last dimension is one greater than that of the input, because we're computing the integral corresponding to the endpoints of a step function, not the integral of the interior/bin values. Args: w: Tensor, which will be integrated along the last axis. This is assumed to sum to 1 along the last axis, and this function will (silently) break if that is not the case. Returns: cw0: Tensor, the integral of w, where cw0[..., 0] = 0 and cw0[..., -1] = 1 """ cw = jnp.minimum(1, jnp.cumsum(w[Ellipsis, :-1], axis=-1)) shape = cw.shape[:-1] + (1,) # Ensure that the CDF starts with exactly 0 and ends with exactly 1. cw0 = jnp.concatenate([jnp.zeros(shape), cw, jnp.ones(shape)], axis=-1) return cw0 def invert_cdf(u, t, w_logits): """Invert the CDF defined by (t, w) at the points specified by u in [0, 1).""" utils.assert_valid_stepfun(t, w_logits) # Compute the PDF and CDF for each weight vector. w = jax.nn.softmax(w_logits, axis=-1) cw = integrate_weights(w) # Interpolate into the inverse CDF. t_new = math.sorted_interp(u, cw, t, utils.device_is_tpu()) return t_new def sample( rng, t, w_logits, num_samples, single_jitter=False, deterministic_center=False, eps=jnp.finfo(jnp.float32).eps, ): """Piecewise-Constant PDF sampling from a step function. Args: rng: random number generator (or None for `linspace` sampling). t: [..., num_bins + 1], bin endpoint coordinates (must be sorted) w_logits: [..., num_bins], logits corresponding to bin weights num_samples: int, the number of samples. single_jitter: bool, if True, jitter every sample along each ray by the same amount in the inverse CDF. Otherwise, jitter each sample independently. deterministic_center: bool, if False, when `rng` is None return samples that linspace the entire PDF. If True, skip the front and back of the linspace so that the centers of each PDF interval are returned. eps: float, something like numerical epsilon. Returns: t_samples: jnp.ndarray(float32), [batch_size, num_samples]. """ utils.assert_valid_stepfun(t, w_logits) # Draw uniform samples. if rng is None: # Match the behavior of jax.random.uniform() by spanning [0, 1-eps]. if deterministic_center: pad = 1 / (2 * num_samples) u = jnp.linspace(pad, 1.0 - pad - eps, num_samples) else: u = jnp.linspace(0, 1.0 - eps, num_samples) u = jnp.broadcast_to(u, t.shape[:-1] + (num_samples,)) else: # `u` is in [0, 1) --- it can be zero, but it can never be 1. u_max = eps + (1 - eps) / num_samples max_jitter = (1 - u_max) / (num_samples - 1) - eps d = 1 if single_jitter else num_samples u = jnp.linspace(0, 1 - u_max, num_samples) + jax.random.uniform( rng, t.shape[:-1] + (d,), maxval=max_jitter ) return invert_cdf(u, t, w_logits) def sample_intervals( rng, t, w_logits, num_samples, single_jitter=False, domain=(-jnp.inf, jnp.inf), ): """Sample *intervals* (rather than points) from a step function. Args: rng: random number generator (or None for `linspace` sampling). t: [..., num_bins + 1], bin endpoint coordinates (must be sorted) w_logits: [..., num_bins], logits corresponding to bin weights num_samples: int, the number of intervals to sample. single_jitter: bool, if True, jitter every sample along each ray by the same amount in the inverse CDF. Otherwise, jitter each sample independently. domain: (minval, maxval), the range of valid values for `t`. Returns: t_samples: jnp.ndarray(float32), [batch_size, num_samples]. """ utils.assert_valid_stepfun(t, w_logits) if num_samples <= 1: raise ValueError(f'num_samples must be > 1, is {num_samples}.') #<fim_suffix><fim_middle> Sample a set of points from the step function.
Sample a set of points from the step function.
LINE_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/ref_utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Functions for reflection directions and directional encodings.""" import math from internal import math as math_lib import jax.numpy as jnp import numpy as np def reflect(viewdirs, normals): """Reflect view directions about normals. The reflection of a vector v about a unit vector n is a vector u such that dot(v, n) = dot(u, n), and dot(u, u) = dot(v, v). The solution to these two equations is u = 2 dot(n, v) n - v. Args: viewdirs: [..., 3] array of view directions. normals: [..., 3] array of normal directions (assumed to be unit vectors). Returns: [..., 3] array of reflection directions. """ return ( 2.0 * jnp.sum(normals * viewdirs, axis=-1, keepdims=True) * normals - viewdirs ) def l2_normalize(x, grad_eps=jnp.finfo(jnp.float32).eps): """Normalize x to unit length along last axis. Normalizing vectors is surprisingly tricky, because you have to address the case where the denominator in the normalization is tiny or zero, in which case gradients will explode. For this reason, we perform two normalizations: in the forward pass, we clamp the denominator with ~1e-40, but in the backward pass we clamp with `grad_eps`, which defaults to ~1e-7. This guarantees that the output of this function is unit norm (unless x is very very small) while preventing exploding gradients. Args: x: The array of values to normalize. grad_eps: The value to clip the squared norm by before division in the backward pass. Returns: A normalized array x / ||x||, normalized along the last axis. """ tiny = jnp.finfo(jnp.float32).tiny grad_eps = jnp.maximum(tiny, grad_eps) denom_sq = jnp.sum(x**2, axis=-1, keepdims=True) normal_val = x / jnp.sqrt(jnp.maximum(tiny, denom_sq)) normal_grad = x / jnp.sqrt(jnp.maximum(grad_eps, denom_sq)) # Use `normal_val` in the forward pass but `normal_grad` in the backward pass. normal = math_lib.override_gradient(normal_val, normal_grad) return jnp.where(denom_sq < tiny, jnp.zeros_like(normal), normal) def compute_weighted_mae(weights, normals, normals_gt): """Compute weighted mean angular error, assuming normals are unit length.""" angles = math_lib.safe_arccos((normals * normals_gt).sum(axis=-1)) return (180.0 / jnp.pi) * ((weights * angles).sum() / weights.sum()) def generalized_binomial_coeff(a, k): """Compute generalized binomial coefficients.""" return np.prod(a - np.arange(k)) / math.factorial(k) def assoc_legendre_coeff(l, m, k): """Compute associated Legendre polynomial coefficients. Returns the coefficient of the cos^k(theta)*sin^m(theta) term in the (l, m)th associated Legendre polynomial, P_l^m(cos(theta)). Args: l: associated Legendre polynomial degree. m: associated Legendre polynomial order. k: power of cos(theta). Returns: A float, the coefficient of the term corresponding to the inputs. """ return ( (-1) ** m * 2**l * math.factorial(l) / math.factorial(k) / math.factorial(l - k - m) * generalized_binomial_coeff(0.5 * (l + k + m - 1.0), l) ) def sph_harm_coeff(l, m, k): """Compute spherical harmonic coefficients.""" return np.sqrt( (2.0 * l + 1.0) * math.factorial(l - m) / (4.0 * np.pi * math.factorial(l + m)) ) * assoc_legendre_coeff(l, m, k) def get_ml_array(deg_view): """Create a list with all pairs of (l, m) values to use in the encoding.""" ml_list = [] for i in range(deg_view): l = 2**i # Only use nonnegative m values, later splitting real and imaginary parts. for m in range(l + 1): ml_list.append((m, l)) # Convert list into a numpy array. ml_array = np.array(ml_list).T return ml_array def generate_ide_fn(deg_view): """Generate integrated directional encoding (IDE) function. This function returns a function that computes the integrated directional encoding from Equations 6-8 of arxiv.org/abs/2112.03907. Args: deg_view: number of spherical harmonics degrees to use. Returns: A function for evaluating integrated directional encoding. Raises: ValueError: if deg_view is larger than 5. """ if deg_view > 5: raise ValueError('Only deg_view of at most 5 is numerically stable.') ml_array = get_ml_array(deg_view) l_max = 2 ** (deg_view - 1) # Create a matrix corresponding to ml_array holding all coefficients, which, # when multiplied (from the right) by the z coordinate Vandermonde matrix, # results in the z component of the encoding. mat = np.zeros((l_max + 1, ml_array.shape[1])) for i, (m, l) in enumerate(ml_array.T): for k in range(l - m + 1): mat[k, i] = sph_harm_coeff(l, m, k) def integrated_dir_enc_fn(xyz, kappa_inv): """Function returning integrated directional encoding (IDE). Args: xyz: [..., 3] array of Cartesian coordinates of directions to evaluate at. kappa_inv: [..., 1] reciprocal of the concentration parameter of the von Mises-Fisher distribution. Returns: An array with the resulting IDE. """ x = xyz[Ellipsis, 0:1] y = xyz[Ellipsis, 1:2] z = xyz[Ellipsis, 2:3] # Compute z Vandermonde matrix. vmz = jnp.concatenate([z**i for i in range(mat.shape[0])], axis=-1) # Compute x+iy Vandermonde matrix. vmxy = jnp.concatenate([(x + 1j * y) ** m for m in ml_array[0, :]], axis=-1) # Get spherical harmonics. sph_harms = vmxy * math_lib.matmul(vmz, mat) # Apply attenuation function using the von Mises-Fisher distribution #<fim_suffix><fim_middle> concentration parameter, kappa.
concentration parameter, kappa.
LINE_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/stepfun.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating step functions (piecewise-constant 1D functions). We have a shared naming and dimension convention for these functions. All input/output step functions are assumed to be aligned along the last axis. `t` always indicates the x coordinates of the *endpoints* of a step function. `y` indicates unconstrained values for the *bins* of a step function `w` indicates bin weights that sum to <= 1. `p` indicates non-negative bin values that *integrate* to <= 1. """ from internal import linspline from internal import math from internal import utils import jax import jax.numpy as jnp import numpy as np def query(tq, t, y, left=None, right=None): """Query step function (t, y) at locations tq. Edges repeat by default.""" utils.assert_valid_stepfun(t, y) # Query the step function to recover the interval value. (i0, i1), ((yq, _),) = math.sorted_lookup(tq, t, (y,), utils.device_is_tpu()) # Apply boundary conditions. left = y[Ellipsis, :1] if left is None else left right = y[Ellipsis, -1:] if right is None else right yq = math.select([(i1 == 0, left), (i0 == y.shape[-1], right)], yq) return yq def weight_to_pdf(t, w): """Turn a vector of weights that sums to 1 into a PDF that integrates to 1.""" utils.assert_valid_stepfun(t, w) td = jnp.diff(t) return jnp.where(td < np.finfo(np.float32).tiny, 0, math.safe_div(w, td)) def pdf_to_weight(t, p): """Turn a PDF that integrates to 1 into a vector of weights that sums to 1.""" utils.assert_valid_stepfun(t, p) return p * jnp.diff(t) def integrate_weights(w): """Compute the cumulative sum of w, assuming all weight vectors sum to 1. The output's size on the last dimension is one greater than that of the input, because we're computing the integral corresponding to the endpoints of a step function, not the integral of the interior/bin values. Args: w: Tensor, which will be integrated along the last axis. This is assumed to sum to 1 along the last axis, and this function will (silently) break if that is not the case. Returns: cw0: Tensor, the integral of w, where cw0[..., 0] = 0 and cw0[..., -1] = 1 """ cw = jnp.minimum(1, jnp.cumsum(w[Ellipsis, :-1], axis=-1)) shape = cw.shape[:-1] + (1,) # Ensure that the CDF starts with exactly 0 and ends with exactly 1. cw0 = jnp.concatenate([jnp.zeros(shape), cw, jnp.ones(shape)], axis=-1) return cw0 def invert_cdf(u, t, w_logits): """Invert the CDF defined by (t, w) at the points specified by u in [0, 1).""" utils.assert_valid_stepfun(t, w_logits) # Compute the PDF and CDF for each weight vector. w = jax.nn.softmax(w_logits, axis=-1) cw = integrate_weights(w) # Interpolate into the inverse CDF. t_new = math.sorted_interp(u, cw, t, utils.device_is_tpu()) return t_new def sample( rng, t, w_logits, num_samples, single_jitter=False, deterministic_center=False, eps=jnp.finfo(jnp.float32).eps, ): """Piecewise-Constant PDF sampling from a step function. Args: rng: random number generator (or None for `linspace` sampling). t: [..., num_bins + 1], bin endpoint coordinates (must be sorted) w_logits: [..., num_bins], logits corresponding to bin weights num_samples: int, the number of samples. single_jitter: bool, if True, jitter every sample along each ray by the same amount in the inverse CDF. Otherwise, jitter each sample independently. deterministic_center: bool, if False, when `rng` is None return samples that linspace the entire PDF. If True, skip the front and back of the linspace so that the centers of each PDF interval are returned. eps: float, something like numerical epsilon. Returns: t_samples: jnp.ndarray(float32), [batch_size, num_samples]. """ utils.assert_valid_stepfun(t, w_logits) # Draw uniform samples. if rng is None: # Match the behavior of jax.random.uniform() by spanning [0, 1-eps]. if deterministic_center: pad = 1 / (2 * num_samples) u = jnp.linspace(pad, 1.0 - pad - eps, num_samples) else: u = jnp.linspace(0, 1.0 - eps, num_samples) u = jnp.broadcast_to(u, t.shape[:-1] + (num_samples,)) else: # `u` is in [0, 1) --- it can be zero, but it can never be 1. u_max = eps + (1 - eps) / num_samples max_jitter = (1 - u_max) / (num_samples - 1) - eps d = 1 if single_jitter else num_samples u = jnp.linspace(0, 1 - u_max, num_samples) + jax.random.uniform( rng, t.shape[:-1] + (d,), maxval=max_jitter ) return invert_cdf(u, t, w_logits) def sample_intervals( rng, t, w_logits, num_samples, single_jitter=False, domain=(-jnp.inf, jnp.inf), ): """Sample *intervals* (rather than points) from a step function. Args: rng: random number generator (or None for `linspace` sampling). t: [..., num_bins + 1], bin endpoint coordinates (must be sorted) w_logits: [..., num_bins], logits corresponding to bin weights num_samples: int, the number of intervals to sample. single_jitter: bool, if True, jitter every sample along each ray by the same amount in the inverse CDF. Otherwise, jitter each sample independently. domain: (minval, maxval), the range of valid values for `t`. Returns: t_samples: jnp.ndarray(float32), [batch_size, num_samples]. """ utils.assert_valid_stepfun(t, w_logits) if num_samples <= 1: raise ValueError(f'num_samples must be > 1, is {num_samples}.') # Sample a set of points from the step function. centers = sample( rng, t, w_logits, num_samples, single_jitter, deterministic_center=True ) # The intervals we return will span the midpoints of each adjacent sample. mid = (centers[Ellipsis, 1:] + centers[Ellipsis, :-1]) / 2 # Each first/last fencepost is the reflection of the first/last midpoint # around the first/last sampled center. first = 2 * centers[Ellipsis, :1] - mid[Ellipsis, :1] last = 2 * centers[Ellipsis, -1:] - mid[Ellipsis, -1:] samples = jnp.concatenate([first, mid, last], axis=-1) # We clamp to the limits of the input domain, provided by the caller. samples = jnp.clip(samples, *domain) return samples def lossfun_distortion(t, w): """Compute iint w[i] w[j] |t[i] - t[j]| di dj.""" utils.assert_valid_stepfun(t, w) # The loss incurred between all pairs of intervals. ut = (t[Ellipsis, 1:] + t[Ellipsis, :-1]) / 2 dut = jnp.abs(ut[Ellipsis, :, None] - ut[Ellipsis, None, :]) loss_inter = jnp.sum(w * jnp.sum(w[Ellipsis, None, :] * dut, axis=-1), axis=-1) #<fim_suffix><fim_middle> The loss incurred within each individual interval with itself.
The loss incurred within each individual interval with itself.
LINE_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/linspline.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Helper functions for linear splines.""" import functools from internal import math from internal import utils import jax from jax.experimental import checkify import jax.numpy as jnp def check_zero_endpoints(y): checkify.check(jnp.all(y[Ellipsis, 0] == 0), 'Splines must all start with 0.') checkify.check(jnp.all(y[Ellipsis, -1] == 0), 'Splines must all end with 0.') def query(tq, t, v): """Query linear spline (t, v) at tq.""" utils.assert_valid_linspline(t, v) interp = functools.partial(jnp.interp, left=0, right=0) return jnp.vectorize(interp, signature='(n),(m),(m)->(n)')(tq, t, v) def integrate(t, w): """Integrate (t, w) according to the trapezoid rule.""" utils.assert_valid_linspline(t, w) return 0.5 * jnp.sum((w[Ellipsis, :-1] + w[Ellipsis, 1:]) * jnp.diff(t), axis=-1) def normalize(t, w, eps=jnp.finfo(jnp.float32).eps ** 2): """Make w integrate to 1.""" utils.assert_valid_linspline(t, w) return w / jnp.maximum(eps, integrate(t, w))[Ellipsis, None] def insert_knot(ti, t, y): """Inserts knots ti into the linear spline (t, w). Assumes zero endpoints.""" utils.assert_valid_linspline(t, y) check_zero_endpoints(y) # Compute the spline value at the insertion points. yi = query(ti, t, y) # Concatenate the insertion points and values onto the end of each spline. ti_ex = jnp.broadcast_to(ti, t.shape[: -len(ti.shape)] + ti.shape) yi_ex = jnp.broadcast_to(yi, y.shape[: -len(yi.shape)] + yi.shape) to = jnp.concatenate([t, ti_ex], axis=-1) yo = jnp.concatenate([y, yi_ex], axis=-1) # Sort the spline according to t. sort_idx = jnp.argsort(to) to = jnp.take_along_axis(to, sort_idx, axis=-1) yo = jnp.take_along_axis(yo, sort_idx, axis=-1) return to, yo def clamp(t, y, minval, maxval): """Clamp (t, y) to be zero outside of t in [minval, maxval].""" utils.assert_valid_linspline(t, y) check_zero_endpoints(y) # Add in extra points at and immediately above/below the min/max vals. ti = jnp.concatenate( [ math.minus_eps(minval), minval, maxval, math.plus_eps(maxval), ], axis=-1, ) tc, yo = insert_knot(ti, t, y) # Zero the spline values outside of [minval, maxval]. yc = jnp.where(tc > maxval, 0, jnp.where(tc < minval, 0, yo)) return tc, yc def compute_integral(t, y): """Integrate a linear spline into a piecewise quadratic spline.""" utils.assert_valid_linspline(t, y) eps = jnp.finfo(jnp.float32).eps ** 2 dt = jnp.diff(t) a = jnp.diff(y) / jnp.maximum(eps, 2 * dt) b = y[Ellipsis, :-1] # The integral has an ambiguous global offset here, which we set to 0. c1 = 0.5 * jnp.cumsum(dt[Ellipsis, :-1] * (y[Ellipsis, :-2] + y[Ellipsis, 1:-1]), axis=-1) c = jnp.concatenate([jnp.zeros_like(y[Ellipsis, :1]), c1], axis=-1) # This quadratic is parameterized as: # (t - t[i])**2 * a[i] + (t - t[i]) * b[i] + c[i] return a, b, c def sorted_lookup(x, xp): """Lookup `x` at sorted locations `xp`.""" # jnp.searchsorted() has slightly different conventions for boundary # handling than the rest of this codebase. idx = jnp.vectorize( functools.partial(jnp.searchsorted, side='right'), signature='(n),(m)->(m)', )(xp, x) idx0 = jnp.maximum(idx - 1, 0) idx1 = jnp.minimum(idx, xp.shape[-1] - 1) return idx0, idx1 def interpolate_integral(tq, t, a, b, c): """Interpolate into the piecewise quadratic returned by compute_integral().""" utils.assert_valid_stepfun(t, a) utils.assert_valid_stepfun(t, b) utils.assert_valid_stepfun(t, c) # Clip to valid inputs (assumes repeating boundaries). tq = jnp.clip(tq, t[Ellipsis, :1], math.minus_eps(t[Ellipsis, -1:])) # Lookup the quadratic coefficients corresponding to each input query. idx0, _ = sorted_lookup(tq, t) # TODO(barron): It might be faster to stack (a, c, b) during generation and # do a single gather. t0 = jnp.take_along_axis(t, idx0, axis=-1) a0 = jnp.take_along_axis(a, idx0, axis=-1) b0 = jnp.take_along_axis(b, idx0, axis=-1) c0 = jnp.take_along_axis(c, idx0, axis=-1) td = tq - t0 v = a0 * td**2 + b0 * td + c0 return v def blur_stepfun(ts, ys, halfwidth): """Convolve a step function (ts, ys) with a box filter of size `halfwidth`.""" utils.assert_valid_stepfun(ts, ys) # Blur each entire step function by a single `halfwidth` value. #<fim_suffix><fim_middle> Dilate the t-values by at least numerical epsilon in each direction.
Dilate the t-values by at least numerical epsilon in each direction.
LINE_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/geopoly.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for constructing geodesic polyhedron, which are used as a basis.""" import itertools import numpy as np def compute_sq_dist(mat0, mat1=None): """Compute the squared Euclidean distance between all pairs of columns.""" if mat1 is None: mat1 = mat0 # Use the fact that ||x - y||^2 == ||x||^2 + ||y||^2 - 2 x^T y. sq_norm0 = np.sum(mat0**2, 0) sq_norm1 = np.sum(mat1**2, 0) sq_dist = sq_norm0[:, None] + sq_norm1[None, :] - 2 * mat0.T @ mat1 sq_dist = np.maximum(0, sq_dist) # Negative values must be numerical errors. return sq_dist def compute_tesselation_weights(v): """Tesselate the vertices of a triangle by a factor of `v`.""" if v < 1: raise ValueError(f'v {v} must be >= 1') int_weights = [] for i in range(v + 1): for j in range(v + 1 - i): int_weights.append((i, j, v - (i + j))) int_weights = np.array(int_weights) weights = int_weights / v #<fim_suffix><fim_middle> Barycentric weights.
Barycentric weights.
LINE_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/coord.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating coordinate spaces and distances along rays.""" from internal import geopoly from internal import math import jax from jax import random import jax.numpy as jnp import numpy as np def contract(x): """Contracts points towards the origin (Eq 10 of arxiv.org/abs/2111.12077).""" # Clamping to 1 produces correct scale inside |x| < 1 x_mag_sq = jnp.maximum(1, jnp.sum(x**2, axis=-1, keepdims=True)) scale = (2 * jnp.sqrt(x_mag_sq) - 1) / x_mag_sq z = scale * x return z def inv_contract(z): """The inverse of contract().""" # Clamping to 1 produces correct scale inside |z| < 1 z_mag_sq = jnp.maximum(1, jnp.sum(z**2, axis=-1, keepdims=True)) inv_scale = 2 * jnp.sqrt(z_mag_sq) - z_mag_sq x = z / inv_scale return x def track_linearize(fn, mean, cov): """Apply function `fn` to a set of means and covariances, ala a Kalman filter. We can analytically transform a Gaussian parameterized by `mean` and `cov` with a function `fn` by linearizing `fn` around `mean`, and taking advantage of the fact that Covar[Ax + y] = A(Covar[x])A^T (see https://cs.nyu.edu/~roweis/notes/gaussid.pdf for details). Args: fn: A function that can be applied to `mean`. mean: a tensor of Gaussian means, where the last axis is the dimension. cov: a tensor of covariances, where the last two axes are the dimensions. Returns: fn_mean: the transformed means. fn_cov: the transformed covariances. """ if (len(mean.shape) + 1) != len(cov.shape): raise ValueError('cov must be non-diagonal') fn_mean, lin_fn = jax.linearize(fn, mean) fn_cov = jax.vmap(lin_fn, -1, -2)(jax.vmap(lin_fn, -1, -2)(cov)) return fn_mean, fn_cov def track_isotropic(fn, mean, scale): """Apply function `fn` to a set of means and scales, ala a Kalman filter. This is the isotropic or scalar equivalent of track_linearize, as we're still linearizing a function and tracking a Gaussian through it, but the input and output Gaussians are all isotropic and are only represented with a single `scale` value (where `scale**2` is the variance of the Gaussian). Args: fn: A function that can be applied to `mean`. mean: a tensor of Gaussian means, where the last axis is the dimension. scale: a tensor of scales, with the same shape as means[..., -1]. Returns: fn_mean: the transformed means. fn_scale: the transformed scales. """ if mean.shape[:-1] != scale.shape: raise ValueError( f'mean.shape[:-1] {mean.shape}[:-1] != scale.shape {scale.shape}.' ) d = mean.shape[-1] fn_mean, lin_fn = jax.linearize(fn, mean) if scale is not None: # Compute the Jacobian of fn function at the locations of each mean. jac = jax.vmap(lin_fn, in_axes=-1, out_axes=-1)( jnp.broadcast_to(jnp.eye(d), mean.shape + (d,)) ) # The cube root of the determinant of the Jacobian is the geometric mean # of the eigenvalues of the Jacobian, which gives us the isotropic scaling # implied by `fn` at each mean that `scale` should be multiplied by. eps = jnp.finfo(jnp.float32).tiny # Guard against an inf gradient at 0. abs_det = jnp.maximum(eps, jnp.abs(jnp.linalg.det(jac))) # Special case d == 3 for speed's sake. fn_scale = scale * (jnp.cbrt(abs_det) if d == 3 else abs_det ** (1 / d)) else: fn_scale = None return fn_mean, fn_scale def contract3_isoscale(x): """A fast version of track_isotropic(contract, *)'s scaling for 3D inputs.""" if x.shape[-1] != 3: raise ValueError(f'Inputs must be 3D, are {x.shape[-1]}D.') norm_sq = jnp.maximum(1, jnp.sum(x**2, axis=-1)) # Equivalent to cbrt((2 * sqrt(norm_sq) - 1) ** 2) / norm_sq: return jnp.exp(2 / 3 * jnp.log(2 * jnp.sqrt(norm_sq) - 1) - jnp.log(norm_sq)) def construct_ray_warps(fn, t_near, t_far, *, fn_inv=None): """Construct a bijection between metric distances and normalized distances. See the text around Equation 11 in https://arxiv.org/abs/2111.12077 for a detailed explanation. Args: fn: the function to ray distances. t_near: a tensor of near-plane distances. t_far: a tensor of far-plane distances. fn_inv: Optional, if not None then it's used as the inverse of fn(). Returns: t_to_s: a function that maps distances to normalized distances in [0, 1]. s_to_t: the inverse of t_to_s. """ if fn is None: fn_fwd = lambda x: x fn_inv = lambda x: x else: fn_fwd = fn if fn_inv is None: # A simple mapping from some functions to their inverse. inv_mapping = { 'reciprocal': jnp.reciprocal, 'log': jnp.exp, 'exp': jnp.log, 'sqrt': jnp.square, 'square': jnp.sqrt, } fn_inv = inv_mapping[fn.__name__] fn_t_near, fn_t_far = [fn_fwd(t) for t in (t_near, t_far)] # Forcibly clip t to the range of valid values, to guard against inf's. t_clip = lambda t: jnp.clip(t, t_near, t_far) t_to_s = lambda t: (fn_fwd(t_clip(t)) - fn_t_near) / (fn_t_far - fn_t_near) s_to_t = lambda s: t_clip(fn_inv(s * fn_t_far + (1 - s) * fn_t_near)) return t_to_s, s_to_t def expected_sin(mean, var): """Compute the mean of sin(x), x ~ N(mean, var).""" return jnp.exp(-0.5 * var) * math.safe_sin(mean) # large var -> small value. def integrated_pos_enc(mean, var, min_deg, max_deg): """Encode `x` with sinusoids scaled by 2^[min_deg, max_deg). Args: mean: tensor, the mean coordinates to be encoded var: tensor, the variance of the coordinates to be encoded. min_deg: int, the min degree of the encoding. max_deg: int, the max degree of the encoding. Returns: encoded: jnp.ndarray, encoded variables. """ scales = 2.0 ** jnp.arange(min_deg, max_deg) shape = mean.shape[:-1] + (-1,) scaled_mean = jnp.reshape(mean[Ellipsis, None, :] * scales[:, None], shape) scaled_var = jnp.reshape(var[Ellipsis, None, :] * scales[:, None] ** 2, shape) return expected_sin( jnp.concatenate([scaled_mean, scaled_mean + 0.5 * jnp.pi], axis=-1), jnp.concatenate([scaled_var] * 2, axis=-1), ) def lift_and_diagonalize(mean, cov, basis): """Project `mean` and `cov` onto basis and diagonalize the projected cov.""" fn_mean = math.matmul(mean, basis) fn_cov_diag = jnp.sum(basis * math.matmul(cov, basis), axis=-2) return fn_mean, fn_cov_diag def pos_enc(x, min_deg, max_deg, append_identity=True): """The positional encoding used by the original NeRF paper.""" scales = 2.0 ** jnp.arange(min_deg, max_deg) shape = x.shape[:-1] + (-1,) scaled_x = x[Ellipsis, None, :] * scales[:, None] # (..., s, c). scaled_x = jnp.reshape(scaled_x, shape) # (..., s*c). # Note that we're not using safe_sin, unlike IPE. # (..., s*c + s*c). four_feat = jnp.sin( jnp.concatenate([scaled_x, scaled_x + 0.5 * jnp.pi], axis=-1) ) if append_identity: return jnp.concatenate([x, four_feat], axis=-1) else: return four_feat def sqrtm(mat, return_eigs=False): """Take the matrix square root of a PSD matrix [..., d, d].""" eigvec, eigval = jax.lax.linalg.eigh( mat, symmetrize_input=False, sort_eigenvalues=False ) scaling = math.safe_sqrt(eigval)[Ellipsis, None, :] sqrtm_mat = math.matmul(eigvec * scaling, jnp.moveaxis(eigvec, -2, -1)) return (sqrtm_mat, (eigvec, eigval)) if return_eigs else sqrtm_mat def isotropize(cov, mode='accurate'): """Turn covariances into isotropic covariances with the same determinant.""" d = cov.shape[-1] if d == 1: return cov if mode == 'fast': det = jnp.linalg.det(cov) diag_val = det ** (1 / d) is_invalid = (det <= jnp.finfo(jnp.float32).tiny) | ~jnp.isfinite(det) elif mode == 'accurate': log_det = jnp.linalg.slogdet(cov)[1] diag_val = jnp.exp(log_det / d) is_invalid = ~jnp.isfinite(log_det) else: raise ValueError(f'mode={mode} not implemented.') cov_iso = jnp.eye(d) * diag_val[Ellipsis, None, None] #<fim_suffix><fim_middle> Guard against NaN outputs when `det` is super small. Note that this does not
Guard against NaN outputs when `det` is super small. Note that this does not
LINE_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/stepfun.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for manipulating step functions (piecewise-constant 1D functions). We have a shared naming and dimension convention for these functions. All input/output step functions are assumed to be aligned along the last axis. `t` always indicates the x coordinates of the *endpoints* of a step function. `y` indicates unconstrained values for the *bins* of a step function `w` indicates bin weights that sum to <= 1. `p` indicates non-negative bin values that *integrate* to <= 1. """ from internal import linspline from internal import math from internal import utils import jax import jax.numpy as jnp import numpy as np def query(tq, t, y, left=None, right=None): """Query step function (t, y) at locations tq. Edges repeat by default.""" utils.assert_valid_stepfun(t, y) # Query the step function to recover the interval value. (i0, i1), ((yq, _),) = math.sorted_lookup(tq, t, (y,), utils.device_is_tpu()) # Apply boundary conditions. left = y[Ellipsis, :1] if left is None else left right = y[Ellipsis, -1:] if right is None else right yq = math.select([(i1 == 0, left), (i0 == y.shape[-1], right)], yq) return yq def weight_to_pdf(t, w): """Turn a vector of weights that sums to 1 into a PDF that integrates to 1.""" utils.assert_valid_stepfun(t, w) td = jnp.diff(t) return jnp.where(td < np.finfo(np.float32).tiny, 0, math.safe_div(w, td)) def pdf_to_weight(t, p): """Turn a PDF that integrates to 1 into a vector of weights that sums to 1.""" utils.assert_valid_stepfun(t, p) return p * jnp.diff(t) def integrate_weights(w): """Compute the cumulative sum of w, assuming all weight vectors sum to 1. The output's size on the last dimension is one greater than that of the input, because we're computing the integral corresponding to the endpoints of a step function, not the integral of the interior/bin values. Args: w: Tensor, which will be integrated along the last axis. This is assumed to sum to 1 along the last axis, and this function will (silently) break if that is not the case. Returns: cw0: Tensor, the integral of w, where cw0[..., 0] = 0 and cw0[..., -1] = 1 """ cw = jnp.minimum(1, jnp.cumsum(w[Ellipsis, :-1], axis=-1)) shape = cw.shape[:-1] + (1,) # Ensure that the CDF starts with exactly 0 and ends with exactly 1. cw0 = jnp.concatenate([jnp.zeros(shape), cw, jnp.ones(shape)], axis=-1) return cw0 def invert_cdf(u, t, w_logits): """Invert the CDF defined by (t, w) at the points specified by u in [0, 1).""" utils.assert_valid_stepfun(t, w_logits) # Compute the PDF and CDF for each weight vector. w = jax.nn.softmax(w_logits, axis=-1) cw = integrate_weights(w) # Interpolate into the inverse CDF. t_new = math.sorted_interp(u, cw, t, utils.device_is_tpu()) return t_new def sample( rng, t, w_logits, num_samples, single_jitter=False, deterministic_center=False, eps=jnp.finfo(jnp.float32).eps, ): """Piecewise-Constant PDF sampling from a step function. Args: rng: random number generator (or None for `linspace` sampling). t: [..., num_bins + 1], bin endpoint coordinates (must be sorted) w_logits: [..., num_bins], logits corresponding to bin weights num_samples: int, the number of samples. single_jitter: bool, if True, jitter every sample along each ray by the same amount in the inverse CDF. Otherwise, jitter each sample independently. deterministic_center: bool, if False, when `rng` is None return samples that linspace the entire PDF. If True, skip the front and back of the linspace so that the centers of each PDF interval are returned. eps: float, something like numerical epsilon. Returns: t_samples: jnp.ndarray(float32), [batch_size, num_samples]. """ utils.assert_valid_stepfun(t, w_logits) # Draw uniform samples. if rng is None: # Match the behavior of jax.random.uniform() by spanning [0, 1-eps]. if deterministic_center: pad = 1 / (2 * num_samples) u = jnp.linspace(pad, 1.0 - pad - eps, num_samples) else: u = jnp.linspace(0, 1.0 - eps, num_samples) u = jnp.broadcast_to(u, t.shape[:-1] + (num_samples,)) else: # `u` is in [0, 1) --- it can be zero, but it can never be 1. u_max = eps + (1 - eps) / num_samples max_jitter = (1 - u_max) / (num_samples - 1) - eps d = 1 if single_jitter else num_samples u = jnp.linspace(0, 1 - u_max, num_samples) + jax.random.uniform( rng, t.shape[:-1] + (d,), maxval=max_jitter ) return invert_cdf(u, t, w_logits) def sample_intervals( rng, t, w_logits, num_samples, single_jitter=False, domain=(-jnp.inf, jnp.inf), ): """Sample *intervals* (rather than points) from a step function. Args: rng: random number generator (or None for `linspace` sampling). t: [..., num_bins + 1], bin endpoint coordinates (must be sorted) w_logits: [..., num_bins], logits corresponding to bin weights num_samples: int, the number of intervals to sample. single_jitter: bool, if True, jitter every sample along each ray by the same amount in the inverse CDF. Otherwise, jitter each sample independently. domain: (minval, maxval), the range of valid values for `t`. Returns: t_samples: jnp.ndarray(float32), [batch_size, num_samples]. """ utils.assert_valid_stepfun(t, w_logits) if num_samples <= 1: raise ValueError(f'num_samples must be > 1, is {num_samples}.') # Sample a set of points from the step function. centers = sample( rng, t, w_logits, num_samples, single_jitter, deterministic_center=True ) # The intervals we return will span the midpoints of each adjacent sample. mid = (centers[Ellipsis, 1:] + centers[Ellipsis, :-1]) / 2 # Each first/last fencepost is the reflection of the first/last midpoint # around the first/last sampled center. first = 2 * centers[Ellipsis, :1] - mid[Ellipsis, :1] last = 2 * centers[Ellipsis, -1:] - mid[Ellipsis, -1:] samples = jnp.concatenate([first, mid, last], axis=-1) # We clamp to the limits of the input domain, provided by the caller. samples = jnp.clip(samples, *domain) return samples def lossfun_distortion(t, w): """Compute iint w[i] w[j] |t[i] - t[j]| di dj.""" utils.assert_valid_stepfun(t, w) #<fim_suffix><fim_middle> The loss incurred between all pairs of intervals.
The loss incurred between all pairs of intervals.
LINE_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/math.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Mathy utility functions.""" import functools import jax import jax.numpy as jnp import numpy as np tiny_val = np.float32(np.finfo(np.float32).tiny) min_val = np.float32(np.finfo(np.float32).min) max_val = np.float32(np.finfo(np.float32).max) def laplace_cdf(x, beta): alpha = 1 / beta return alpha * (0.5 + 0.5 * safe_sign(x) * (jnp.exp(-jnp.abs(x) / beta) - 1)) def scaled_softplus(x, scale=100.0): return (1.0 / scale) * jax.nn.softplus(scale * x) def matmul(a, b): """jnp.matmul defaults to bfloat16, but this helper function doesn't.""" return jnp.matmul(a, b, precision=jax.lax.Precision.HIGHEST) def unstack(x, axis=0): return tuple( jnp.squeeze(z, axis=axis) for z in jnp.split(x, x.shape[axis], axis=axis) ) @jax.custom_jvp def plus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, tiny_val, jnp.nextafter(jnp.float32(x), jnp.inf) ) @jax.custom_jvp def minus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, -tiny_val, jnp.nextafter(jnp.float32(x), -jnp.inf) ) @plus_eps.defjvp def plus_eps_jvp(primals, tangents): """Make plus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return plus_eps(*primals), tangents[0] @minus_eps.defjvp def minus_eps_jvp(primals, tangents): """Make minus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return minus_eps(*primals), tangents[0] @jax.custom_jvp def expm1(x): """jnp.expm1() has inaccurate gradients when x << 0, this doesn't.""" return jnp.expm1(x) @expm1.defjvp def expm1_jvp(primals, tangents): return expm1(*primals), tangents[0] * jnp.exp(primals[0]) def safe_trig_helper(x, fn, t=100 * jnp.pi): """Helper function used by safe_cos/safe_sin: mods x before sin()/cos().""" return fn(jnp.nan_to_num(jnp.where(jnp.abs(x) < t, x, x % t))) def safe_cos(x): """jnp.cos() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.cos) def safe_sin(x): """jnp.sin() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.sin) @jax.custom_vjp def safe_arctan2(x1, x2): return safe_arctan2_fwd(x1, x2)[0] def safe_arctan2_fwd(x1, x2): return jnp.arctan2(x1, x2), (x1, x2) def safe_arctan2_bwd(res, g): x1, x2 = res denom = remove_zero(x1**2 + x2**2) d1 = g * (x2 / denom) d2 = g * (-x1 / denom) return d1, d2 safe_arctan2.defvjp(safe_arctan2_fwd, safe_arctan2_bwd) def generate_clip_nograd_fn(a_min, a_max): """Generates a function that clips to [a_min, a_max] with no grad effects.""" @jax.custom_jvp def clip_nograd(a): """Clamps `a` from above and below.""" return jnp.clip(a, a_min, a_max) @clip_nograd.defjvp def clip_nograd_jvp(primals, tangents): """Override clips()'s gradient to be a no-op.""" return clip_nograd(primals[0]), tangents[0] return clip_nograd clip_finite_nograd = generate_clip_nograd_fn(min_val, max_val) clip_pos_finite_nograd = generate_clip_nograd_fn(tiny_val, max_val) def clip_pos(x): """Clamps `x` from below to be positive.""" return jnp.maximum(tiny_val, x) def safe_sign(x): """jnp.sign(x) except x=0 is assumed to have a sign of +1, not 0.""" return jnp.where(x < 0, -1, +1) def remove_zero(x): """Shifts `x` away from 0.""" return jnp.where(jnp.abs(x) < tiny_val, tiny_val, x) def clip_finite(x): return jnp.clip(x, min_val, max_val) @jax.custom_vjp def safe_div(n, d): """Divide `n` by `d` but the value and gradient never nan out.""" return safe_div_fwd(n, d)[0] def safe_div_fwd(n, d): r = jnp.clip(n / remove_zero(d), min_val, max_val) return jnp.where(jnp.abs(d) < tiny_val, 0, r), (d, r) def safe_div_bwd(res, g): d, r = res dn = jnp.clip(g / remove_zero(d), min_val, max_val) dd = jnp.clip(-g * r / remove_zero(d), min_val, max_val) return dn, dd safe_div.defvjp(safe_div_fwd, safe_div_bwd) def generate_safe_fn(fn, grad_fn, x_range): """Generate's a `safe` fn() where inputs are clipped in fwd and bwd passes.""" @jax.custom_jvp def safe_fn(x): """fn() with clipped inputs.""" return fn(jnp.clip(x, *x_range)) @safe_fn.defjvp def safe_fn_jvp(primals, tangents): """Backpropagate using the gradient and clipped inputs.""" (x,) = primals (x_dot,) = tangents y = safe_fn(x) y_dot = grad_fn(jnp.clip(x, *x_range), y, x_dot) return y, y_dot return safe_fn # These safe_* functions need to be wrapped in no-op function definitions for # gin to recognize them, otherwise they could just be calls to generate_safe_fn. def safe_log(x): return generate_safe_fn( jnp.log, lambda x, _, x_dot: x_dot / x, (tiny_val, max_val), )(x) def safe_exp(x): return generate_safe_fn( jnp.exp, lambda _, y, x_dot: y * x_dot, (min_val, np.nextafter(np.log(max_val), np.float32(0))), )(x) def safe_sqrt(x): return generate_safe_fn( jnp.sqrt, lambda x, _, x_dot: 0.5 * x_dot / jnp.sqrt(jnp.maximum(tiny_val, x)), (0, max_val), )(x) def safe_log1p(x): return generate_safe_fn( jnp.log1p, lambda x, _, x_dot: x_dot / (1 + x), (np.nextafter(np.float32(-1), np.float32(0)), max_val), )(x) def safe_expm1(x): return generate_safe_fn( expm1, # Note that we wrap around our more accurate expm1. lambda x, _, x_dot: jnp.exp(x) * x_dot, (min_val, np.nextafter(np.log1p(max_val), np.float32(0))), )(x) def safe_arccos(x): """jnp.arccos(x) where x is clipped to [-1, 1].""" y = jnp.arccos(jnp.clip(x, plus_eps(-1), minus_eps(1))) return jnp.where(x >= 1, 0, jnp.where(x <= -1, jnp.pi, y)) def apply_fn_to_grad(grad_fn): """Applies a scalar `grad_fn` function to the gradient of the input.""" @jax.custom_vjp def fn_out(x): return x fn_out.defvjp(lambda x: (x, None), lambda _, y: (grad_fn(y),)) return fn_out def select(cond_pairs, default): """A helpful wrapper around jnp.select() that is easier to read.""" return jnp.select(*zip(*cond_pairs), default) def power_ladder_max_output(p): """The limit of power_ladder(x, p) as x goes to infinity.""" return select( [ (p == -jnp.inf, 1), (p >= 0, jnp.inf), ], safe_div(p - 1, p), ) def power_ladder(x, p, premult=None, postmult=None): """Tukey's power ladder, with a +1 on x, some scaling, and special cases.""" # Compute sign(x) * |p - 1|/p * ((|x|/|p-1| + 1)^p - 1) if premult is not None: x = x * premult xp = jnp.abs(x) xs = xp / jnp.maximum(tiny_val, jnp.abs(p - 1)) p_safe = clip_finite_nograd(remove_zero(p)) y = safe_sign(x) * select( [ (p == 1, xp), (p == 0, safe_log1p(xp)), (p == -jnp.inf, -safe_expm1(-xp)), (p == jnp.inf, safe_expm1(xp)), ], clip_finite_nograd( jnp.abs(p_safe - 1) / p_safe * ((xs + 1) ** p_safe - 1) ), ) if postmult is not None: y = y * postmult return y def inv_power_ladder(y, p, premult=None, postmult=None): """The inverse of `power_ladder()`.""" if postmult is not None: y /= postmult yp = jnp.abs(y) p_safe = clip_finite_nograd(remove_zero(p)) y_max = minus_eps(power_ladder_max_output(p)) yp = override_gradient(jnp.clip(yp, -y_max, y_max), yp) # Clip val, not grad. x = safe_sign(y) * select( [ (p == 1, yp), (p == 0, safe_expm1(yp)), (p == -jnp.inf, -safe_log1p(-yp)), (p == jnp.inf, safe_log1p(yp)), ], jnp.abs(p_safe - 1) * ( ((safe_div(p_safe, jnp.abs(p_safe - 1)) * yp + 1)) ** (1 / p_safe) - 1 ), ) if premult is not None: x /= premult return x def log_lerp(t, v0, v1): """Interpolate log-linearly from `v0` (t=0) to `v1` (t=1).""" if v0 <= 0 or v1 <= 0: raise ValueError(f'Interpolants {v0} and {v1} must be positive.') lv0 = jnp.log(v0) lv1 = jnp.log(v1) return jnp.exp(jnp.clip(t, 0, 1) * (lv1 - lv0) + lv0) def approx_erf(x): """An approximation of erf() that is accurate to within 0.007.""" return jnp.sign(x) * jnp.sqrt(1 - jnp.exp(-(4 / jnp.pi) * x**2)) def create_learning_rate_decay(**kwargs): """A partial evaluation of learning rate decay that can be used with gin.""" return functools.partial(learning_rate_decay, **kwargs) def learning_rate_decay( step, lr_init, lr_final, max_steps, lr_delay_steps=0, lr_delay_mult=1 ): """Continuous learning rate decay function. The returned rate is lr_init when step=0 and lr_final when step=max_steps, and is log-linearly interpolated elsewhere (equivalent to exponential decay). If lr_delay_steps>0 then the learning rate will be scaled by some smooth function of lr_delay_mult, such that the initial learning rate is lr_init*lr_delay_mult at the beginning of optimization but will be eased back to the normal learning rate when steps>lr_delay_steps. Args: step: int, the current optimization step. lr_init: float, the initial learning rate. lr_final: float, the final learning rate. max_steps: int, the number of steps during optimization. lr_delay_steps: int, the number of steps to delay the full learning rate. lr_delay_mult: float, the multiplier on the rate when delaying it. Returns: lr: the learning for current step 'step'. """ if lr_delay_steps > 0: # A kind of reverse cosine decay. delay_rate = lr_delay_mult + (1 - lr_delay_mult) * jnp.sin( 0.5 * jnp.pi * jnp.clip(step / lr_delay_steps, 0, 1) ) else: delay_rate = 1.0 return delay_rate * log_lerp(step / max_steps, lr_init, lr_final) def sorted_lookup(x, xp, fps, device_is_tpu): """Lookup `x` into locations `xp` , return indices and each `[fp]` value.""" if not isinstance(fps, tuple): raise ValueError(f'Input `fps` must be a tuple, but is {type(fps)}.') if device_is_tpu: # Identify the location in `xp` that corresponds to each `x`. # The final `True` index in `mask` is the start of the matching interval. mask = x[Ellipsis, None, :] >= xp[Ellipsis, :, None] def find_interval(x): # Grab the value where `mask` switches from True to False, and vice versa. # This approach takes advantage of the fact that `x` is sorted. x0 = jnp.max(jnp.where(mask, x[Ellipsis, None], x[Ellipsis, :1, None]), -2) x1 = jnp.min(jnp.where(~mask, x[Ellipsis, None], x[Ellipsis, -1:, None]), -2) return x0, x1 idx0, idx1 = find_interval(jnp.arange(xp.shape[-1])) vals = [find_interval(fp) for fp in fps] else: #<fim_suffix><fim_middle> jnp.searchsorted() has slightly different conventions for boundary
jnp.searchsorted() has slightly different conventions for boundary
LINE_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop #<fim_suffix><fim_middle> iterations
iterations
LINE_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/geopoly.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for constructing geodesic polyhedron, which are used as a basis.""" import itertools import numpy as np def compute_sq_dist(mat0, mat1=None): """Compute the squared Euclidean distance between all pairs of columns.""" if mat1 is None: mat1 = mat0 # Use the fact that ||x - y||^2 == ||x||^2 + ||y||^2 - 2 x^T y. sq_norm0 = np.sum(mat0**2, 0) sq_norm1 = np.sum(mat1**2, 0) sq_dist = sq_norm0[:, None] + sq_norm1[None, :] - 2 * mat0.T @ mat1 sq_dist = np.maximum(0, sq_dist) # Negative values must be numerical errors. return sq_dist def compute_tesselation_weights(v): """Tesselate the vertices of a triangle by a factor of `v`.""" if v < 1: raise ValueError(f'v {v} must be >= 1') int_weights = [] for<fim_suffix><fim_middle> i in range(v + 1): for j in range(v + 1 - i): int_weights.append((i, j, v - (i + j)))
i in range(v + 1): for j in range(v + 1 - i): int_weights.append((i, j, v - (i + j)))
FOR
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/ref_utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Functions for reflection directions and directional encodings.""" import math from internal import math as math_lib import jax.numpy as jnp import numpy as np def reflect(viewdirs, normals): """Reflect view directions about normals. The reflection of a vector v about a unit vector n is a vector u such that dot(v, n) = dot(u, n), and dot(u, u) = dot(v, v). The solution to these two equations is u = 2 dot(n, v) n - v. Args: viewdirs: [..., 3] array of view directions. normals: [..., 3] array of normal directions (assumed to be unit vectors). Returns: [..., 3] array of reflection directions. """ return ( 2.0 * jnp.sum(normals * viewdirs, axis=-1, keepdims=True) * normals - viewdirs ) def l2_normalize(x, grad_eps=jnp.finfo(jnp.float32).eps): """Normalize x to unit length along last axis. Normalizing vectors is surprisingly tricky, because you have to address the case where the denominator in the normalization is tiny or zero, in which case gradients will explode. For this reason, we perform two normalizations: in the forward pass, we clamp the denominator with ~1e-40, but in the backward pass we clamp with `grad_eps`, which defaults to ~1e-7. This guarantees that the output of this function is unit norm (unless x is very very small) while preventing exploding gradients. Args: x: The array of values to normalize. grad_eps: The value to clip the squared norm by before division in the backward pass. Returns: A normalized array x / ||x||, normalized along the last axis. """ tiny = jnp.finfo(jnp.float32).tiny grad_eps = jnp.maximum(tiny, grad_eps) denom_sq = jnp.sum(x**2, axis=-1, keepdims=True) normal_val = x / jnp.sqrt(jnp.maximum(tiny, denom_sq)) normal_grad = x / jnp.sqrt(jnp.maximum(grad_eps, denom_sq)) # Use `normal_val` in the forward pass but `normal_grad` in the backward pass. normal = math_lib.override_gradient(normal_val, normal_grad) return jnp.where(denom_sq < tiny, jnp.zeros_like(normal), normal) def compute_weighted_mae(weights, normals, normals_gt): """Compute weighted mean angular error, assuming normals are unit length.""" angles = math_lib.safe_arccos((normals * normals_gt).sum(axis=-1)) return (180.0 / jnp.pi) * ((weights * angles).sum() / weights.sum()) def generalized_binomial_coeff(a, k): """Compute generalized binomial coefficients.""" return np.prod(a - np.arange(k)) / math.factorial(k) def assoc_legendre_coeff(l, m, k): """Compute associated Legendre polynomial coefficients. Returns the coefficient of the cos^k(theta)*sin^m(theta) term in the (l, m)th associated Legendre polynomial, P_l^m(cos(theta)). Args: l: associated Legendre polynomial degree. m: associated Legendre polynomial order. k: power of cos(theta). Returns: A float, the coefficient of the term corresponding to the inputs. """ return ( (-1) ** m * 2**l * math.factorial(l) / math.factorial(k) / math.factorial(l - k - m) * generalized_binomial_coeff(0.5 * (l + k + m - 1.0), l) ) def sph_harm_coeff(l, m, k): """Compute spherical harmonic coefficients.""" return np.sqrt( (2.0 * l + 1.0) * math.factorial(l - m) / (4.0 * np.pi * math.factorial(l + m)) ) * assoc_legendre_coeff(l, m, k) def get_ml_array(deg_view): """Create a list with all pairs of (l, m) values to use in the encoding.""" ml_list = [] for<fim_suffix><fim_middle> i in range(deg_view): l = 2**i # Only use nonnegative m values, later splitting real and imaginary parts. for m in range(l + 1): ml_list.append((m, l))
i in range(deg_view): l = 2**i # Only use nonnegative m values, later splitting real and imaginary parts. for m in range(l + 1): ml_list.append((m, l))
FOR
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/geopoly.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for constructing geodesic polyhedron, which are used as a basis.""" import itertools import numpy as np def compute_sq_dist(mat0, mat1=None): """Compute the squared Euclidean distance between all pairs of columns.""" if mat1 is None: mat1 = mat0 # Use the fact that ||x - y||^2 == ||x||^2 + ||y||^2 - 2 x^T y. sq_norm0 = np.sum(mat0**2, 0) sq_norm1 = np.sum(mat1**2, 0) sq_dist = sq_norm0[:, None] + sq_norm1[None, :] - 2 * mat0.T @ mat1 sq_dist = np.maximum(0, sq_dist) # Negative values must be numerical errors. return sq_dist def compute_tesselation_weights(v): """Tesselate the vertices of a triangle by a factor of `v`.""" if v < 1: raise ValueError(f'v {v} must be >= 1') int_weights = [] for i in range(v + 1): for<fim_suffix><fim_middle> j in range(v + 1 - i): int_weights.append((i, j, v - (i + j)))
j in range(v + 1 - i): int_weights.append((i, j, v - (i + j)))
FOR
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for<fim_suffix><fim_middle> item in fn(*args, **kwargs): results_queue.put(item)
item in fn(*args, **kwargs): results_queue.put(item)
FOR
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/ref_utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Functions for reflection directions and directional encodings.""" import math from internal import math as math_lib import jax.numpy as jnp import numpy as np def reflect(viewdirs, normals): """Reflect view directions about normals. The reflection of a vector v about a unit vector n is a vector u such that dot(v, n) = dot(u, n), and dot(u, u) = dot(v, v). The solution to these two equations is u = 2 dot(n, v) n - v. Args: viewdirs: [..., 3] array of view directions. normals: [..., 3] array of normal directions (assumed to be unit vectors). Returns: [..., 3] array of reflection directions. """ return ( 2.0 * jnp.sum(normals * viewdirs, axis=-1, keepdims=True) * normals - viewdirs ) def l2_normalize(x, grad_eps=jnp.finfo(jnp.float32).eps): """Normalize x to unit length along last axis. Normalizing vectors is surprisingly tricky, because you have to address the case where the denominator in the normalization is tiny or zero, in which case gradients will explode. For this reason, we perform two normalizations: in the forward pass, we clamp the denominator with ~1e-40, but in the backward pass we clamp with `grad_eps`, which defaults to ~1e-7. This guarantees that the output of this function is unit norm (unless x is very very small) while preventing exploding gradients. Args: x: The array of values to normalize. grad_eps: The value to clip the squared norm by before division in the backward pass. Returns: A normalized array x / ||x||, normalized along the last axis. """ tiny = jnp.finfo(jnp.float32).tiny grad_eps = jnp.maximum(tiny, grad_eps) denom_sq = jnp.sum(x**2, axis=-1, keepdims=True) normal_val = x / jnp.sqrt(jnp.maximum(tiny, denom_sq)) normal_grad = x / jnp.sqrt(jnp.maximum(grad_eps, denom_sq)) # Use `normal_val` in the forward pass but `normal_grad` in the backward pass. normal = math_lib.override_gradient(normal_val, normal_grad) return jnp.where(denom_sq < tiny, jnp.zeros_like(normal), normal) def compute_weighted_mae(weights, normals, normals_gt): """Compute weighted mean angular error, assuming normals are unit length.""" angles = math_lib.safe_arccos((normals * normals_gt).sum(axis=-1)) return (180.0 / jnp.pi) * ((weights * angles).sum() / weights.sum()) def generalized_binomial_coeff(a, k): """Compute generalized binomial coefficients.""" return np.prod(a - np.arange(k)) / math.factorial(k) def assoc_legendre_coeff(l, m, k): """Compute associated Legendre polynomial coefficients. Returns the coefficient of the cos^k(theta)*sin^m(theta) term in the (l, m)th associated Legendre polynomial, P_l^m(cos(theta)). Args: l: associated Legendre polynomial degree. m: associated Legendre polynomial order. k: power of cos(theta). Returns: A float, the coefficient of the term corresponding to the inputs. """ return ( (-1) ** m * 2**l * math.factorial(l) / math.factorial(k) / math.factorial(l - k - m) * generalized_binomial_coeff(0.5 * (l + k + m - 1.0), l) ) def sph_harm_coeff(l, m, k): """Compute spherical harmonic coefficients.""" return np.sqrt( (2.0 * l + 1.0) * math.factorial(l - m) / (4.0 * np.pi * math.factorial(l + m)) ) * assoc_legendre_coeff(l, m, k) def get_ml_array(deg_view): """Create a list with all pairs of (l, m) values to use in the encoding.""" ml_list = [] for i in range(deg_view): l = 2**i # Only use nonnegative m values, later splitting real and imaginary parts. for m in range(l + 1): ml_list.append((m, l)) # Convert list into a numpy array. ml_array = np.array(ml_list).T return ml_array def generate_ide_fn(deg_view): """Generate integrated directional encoding (IDE) function. This function returns a function that computes the integrated directional encoding from Equations 6-8 of arxiv.org/abs/2112.03907. Args: deg_view: number of spherical harmonics degrees to use. Returns: A function for evaluating integrated directional encoding. Raises: ValueError: if deg_view is larger than 5. """ if deg_view > 5: raise ValueError('Only deg_view of at most 5 is numerically stable.') ml_array = get_ml_array(deg_view) l_max = 2 ** (deg_view - 1) # Create a matrix corresponding to ml_array holding all coefficients, which, # when multiplied (from the right) by the z coordinate Vandermonde matrix, # results in the z component of the encoding. mat = np.zeros((l_max + 1, ml_array.shape[1])) for i, (m, l) in enumerate(ml_array.T): for<fim_suffix><fim_middle> k in range(l - m + 1): mat[k, i] = sph_harm_coeff(l, m, k)
k in range(l - m + 1): mat[k, i] = sph_harm_coeff(l, m, k)
FOR
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/math.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Mathy utility functions.""" import functools import jax import jax.numpy as jnp import numpy as np tiny_val = np.float32(np.finfo(np.float32).tiny) min_val = np.float32(np.finfo(np.float32).min) max_val = np.float32(np.finfo(np.float32).max) def laplace_cdf(x, beta): alpha = 1 / beta return alpha * (0.5 + 0.5 * safe_sign(x) * (jnp.exp(-jnp.abs(x) / beta) - 1)) def scaled_softplus(x, scale=100.0): return (1.0 / scale) * jax.nn.softplus(scale * x) def matmul(a, b): """jnp.matmul defaults to bfloat16, but this helper function doesn't.""" return jnp.matmul(a, b, precision=jax.lax.Precision.HIGHEST) def unstack(x, axis=0): return tuple( jnp.squeeze(z, axis=axis) for z in jnp.split(x, x.shape[axis], axis=axis) ) @jax.custom_jvp def plus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, tiny_val, jnp.nextafter(jnp.float32(x), jnp.inf) ) @jax.custom_jvp def minus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, -tiny_val, jnp.nextafter(jnp.float32(x), -jnp.inf) ) @plus_eps.defjvp def plus_eps_jvp(primals, tangents): """Make plus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return plus_eps(*primals), tangents[0] @minus_eps.defjvp def minus_eps_jvp(primals, tangents): """Make minus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return minus_eps(*primals), tangents[0] @jax.custom_jvp def expm1(x): """jnp.expm1() has inaccurate gradients when x << 0, this doesn't.""" return jnp.expm1(x) @expm1.defjvp def expm1_jvp(primals, tangents): return expm1(*primals), tangents[0] * jnp.exp(primals[0]) def safe_trig_helper(x, fn, t=100 * jnp.pi): """Helper function used by safe_cos/safe_sin: mods x before sin()/cos().""" return fn(jnp.nan_to_num(jnp.where(jnp.abs(x) < t, x, x % t))) def safe_cos(x): """jnp.cos() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.cos) def safe_sin(x): """jnp.sin() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.sin) @jax.custom_vjp def safe_arctan2(x1, x2): return safe_arctan2_fwd(x1, x2)[0] def safe_arctan2_fwd(x1, x2): return jnp.arctan2(x1, x2), (x1, x2) def safe_arctan2_bwd(res, g): x1, x2 = res denom = remove_zero(x1**2 + x2**2) d1 = g * (x2 / denom) d2 = g * (-x1 / denom) return d1, d2 safe_arctan2.defvjp(safe_arctan2_fwd, safe_arctan2_bwd) def generate_clip_nograd_fn(a_min, a_max): """Generates a function that clips to [a_min, a_max] with no grad effects.""" @jax.custom_jvp def clip_nograd(a): """Clamps `a` from above and below.""" return jnp.clip(a, a_min, a_max) @clip_nograd.defjvp def clip_nograd_jvp(primals, tangents): """Override clips()'s gradient to be a no-op.""" return clip_nograd(primals[0]), tangents[0] return clip_nograd clip_finite_nograd = generate_clip_nograd_fn(min_val, max_val) clip_pos_finite_nograd = generate_clip_nograd_fn(tiny_val, max_val) def clip_pos(x): """Clamps `x` from below to be positive.""" return jnp.maximum(tiny_val, x) def safe_sign(x): """jnp.sign(x) except x=0 is assumed to have a sign of +1, not 0.""" return jnp.where(x < 0, -1, +1) def remove_zero(x): """Shifts `x` away from 0.""" return jnp.where(jnp.abs(x) < tiny_val, tiny_val, x) def clip_finite(x): return jnp.clip(x, min_val, max_val) @jax.custom_vjp def safe_div(n, d): """Divide `n` by `d` but the value and gradient never nan out.""" return safe_div_fwd(n, d)[0] def safe_div_fwd(n, d): r = jnp.clip(n / remove_zero(d), min_val, max_val) return jnp.where(jnp.abs(d) < tiny_val, 0, r), (d, r) def safe_div_bwd(res, g): d, r = res dn = jnp.clip(g / remove_zero(d), min_val, max_val) dd = jnp.clip(-g * r / remove_zero(d), min_val, max_val) return dn, dd safe_div.defvjp(safe_div_fwd, safe_div_bwd) def generate_safe_fn(fn, grad_fn, x_range): """Generate's a `safe` fn() where inputs are clipped in fwd and bwd passes.""" @jax.custom_jvp def safe_fn(x): """fn() with clipped inputs.""" return fn(jnp.clip(x, *x_range)) @safe_fn.defjvp def safe_fn_jvp(primals, tangents): """Backpropagate using the gradient and clipped inputs.""" (x,) = primals (x_dot,) = tangents y = safe_fn(x) y_dot = grad_fn(jnp.clip(x, *x_range), y, x_dot) return y, y_dot return safe_fn # These safe_* functions need to be wrapped in no-op function definitions for # gin to recognize them, otherwise they could just be calls to generate_safe_fn. def safe_log(x): return generate_safe_fn( jnp.log, lambda x, _, x_dot: x_dot / x, (tiny_val, max_val), )(x) def safe_exp(x): return generate_safe_fn( jnp.exp, lambda _, y, x_dot: y * x_dot, (min_val, np.nextafter(np.log(max_val), np.float32(0))), )(x) def safe_sqrt(x): return generate_safe_fn( jnp.sqrt, lambda x, _, x_dot: 0.5 * x_dot / jnp.sqrt(jnp.maximum(tiny_val, x)), (0, max_val), )(x) def safe_log1p(x): return generate_safe_fn( jnp.log1p, lambda x, _, x_dot: x_dot / (1 + x), (np.nextafter(np.float32(-1), np.float32(0)), max_val), )(x) def safe_expm1(x): return generate_safe_fn( expm1, # Note that we wrap around our more accurate expm1. lambda x, _, x_dot: jnp.exp(x) * x_dot, (min_val, np.nextafter(np.log1p(max_val), np.float32(0))), )(x) def safe_arccos(x): """jnp.arccos(x) where x is clipped to [-1, 1].""" y = jnp.arccos(jnp.clip(x, plus_eps(-1), minus_eps(1))) return jnp.where(x >= 1, 0, jnp.where(x <= -1, jnp.pi, y)) def apply_fn_to_grad(grad_fn): """Applies a scalar `grad_fn` function to the gradient of the input.""" @jax.custom_vjp def fn_out(x): return x fn_out.defvjp(lambda x: (x, None), lambda _, y: (grad_fn(y),)) return fn_out def select(cond_pairs, default): """A helpful wrapper around jnp.select() that is easier to read.""" return jnp.select(*zip(*cond_pairs), default) def power_ladder_max_output(p): """The limit of power_ladder(x, p) as x goes to infinity.""" return select( [ (p == -jnp.inf, 1), (p >= 0, jnp.inf), ], safe_div(p - 1, p), ) def power_ladder(x, p, premult=None, postmult=None): """Tukey's power ladder, with a +1 on x, some scaling, and special cases.""" # Compute sign(x) * |p - 1|/p * ((|x|/|p-1| + 1)^p - 1) if premult is not None: x = x * premult xp = jnp.abs(x) xs = xp / jnp.maximum(tiny_val, jnp.abs(p - 1)) p_safe = clip_finite_nograd(remove_zero(p)) y = safe_sign(x) * select( [ (p == 1, xp), (p == 0, safe_log1p(xp)), (p == -jnp.inf, -safe_expm1(-xp)), (p == jnp.inf, safe_expm1(xp)), ], clip_finite_nograd( jnp.abs(p_safe - 1) / p_safe * ((xs + 1) ** p_safe - 1) ), ) if postmult is not None: y = y * postmult return y def inv_power_ladder(y, p, premult=None, postmult=None): """The inverse of `power_ladder()`.""" if postmult is not None: y /= postmult yp = jnp.abs(y) p_safe = clip_finite_nograd(remove_zero(p)) y_max = minus_eps(power_ladder_max_output(p)) yp = override_gradient(jnp.clip(yp, -y_max, y_max), yp) # Clip val, not grad. x = safe_sign(y) * select( [ (p == 1, yp), (p == 0, safe_expm1(yp)), (p == -jnp.inf, -safe_log1p(-yp)), (p == jnp.inf, safe_log1p(yp)), ], jnp.abs(p_safe - 1) * ( ((safe_div(p_safe, jnp.abs(p_safe - 1)) * yp + 1)) ** (1 / p_safe) - 1 ), ) if premult is not None: x /= premult return x def log_lerp(t, v0, v1): """Interpolate log-linearly from `v0` (t=0) to `v1` (t=1).""" if v0 <= 0 or v1 <= 0: raise ValueError(f'Interpolants {v0} and {v1} must be positive.') lv0 = jnp.log(v0) lv1 = jnp.log(v1) return jnp.exp(jnp.clip(t, 0, 1) * (lv1 - lv0) + lv0) def approx_erf(x): """An approximation of erf() that is accurate to within 0.007.""" return jnp.sign(x) * jnp.sqrt(1 - jnp.exp(-(4 / jnp.pi) * x**2)) def create_learning_rate_decay(**kwargs): """A partial evaluation of learning rate decay that can be used with gin.""" return functools.partial(learning_rate_decay, **kwargs) def learning_rate_decay( step, lr_init, lr_final, max_steps, lr_delay_steps=0, lr_delay_mult=1 ): """Continuous learning rate decay function. The returned rate is lr_init when step=0 and lr_final when step=max_steps, and is log-linearly interpolated elsewhere (equivalent to exponential decay). If lr_delay_steps>0 then the learning rate will be scaled by some smooth function of lr_delay_mult, such that the initial learning rate is lr_init*lr_delay_mult at the beginning of optimization but will be eased back to the normal learning rate when steps>lr_delay_steps. Args: step: int, the current optimization step. lr_init: float, the initial learning rate. lr_final: float, the final learning rate. max_steps: int, the number of steps during optimization. lr_delay_steps: int, the number of steps to delay the full learning rate. lr_delay_mult: float, the multiplier on the rate when delaying it. Returns: lr: the learning for current step 'step'. """ if lr_delay_steps > 0: # A kind of reverse cosine decay. delay_rate = lr_delay_mult + (1 - lr_delay_mult) * jnp.sin( 0.5 * jnp.pi * jnp.clip(step / lr_delay_steps, 0, 1) ) else: delay_rate = 1.0 return delay_rate * log_lerp(step / max_steps, lr_init, lr_final) def sorted_lookup(x, xp, fps, device_is_tpu): """Lookup `x` into locations `xp` , return indices and each `[fp]` value.""" if not isinstance(fps, tuple): raise ValueError(f'Input `fps` must be a tuple, but is {type(fps)}.') if device_is_tpu: # Identify the location in `xp` that corresponds to each `x`. # The final `True` index in `mask` is the start of the matching interval. mask = x[Ellipsis, None, :] >= xp[Ellipsis, :, None] def find_interval(x): # Grab the value where `mask` switches from True to False, and vice versa. # This approach takes advantage of the fact that `x` is sorted. x0 = jnp.max(jnp.where(mask, x[Ellipsis, None], x[Ellipsis, :1, None]), -2) x1 = jnp.min(jnp.where(~mask, x[Ellipsis, None], x[Ellipsis, -1:, None]), -2) return x0, x1 idx0, idx1 = find_interval(jnp.arange(xp.shape[-1])) vals = [find_interval(fp) for fp in fps] else: # jnp.searchsorted() has slightly different conventions for boundary # handling than the rest of this codebase. idx = jnp.vectorize( lambda a, v: jnp.searchsorted(a, v, side='right'), signature='(n),(m)->(m)', )(xp, x) idx1 = jnp.minimum(idx, xp.shape[-1] - 1) idx0 = jnp.maximum(idx - 1, 0) vals = [] for<fim_suffix><fim_middle> fp in fps: fp0 = jnp.take_along_axis(fp, idx0, axis=-1) fp1 = jnp.take_along_axis(fp, idx1, axis=-1) vals.append((fp0, fp1))
fp in fps: fp0 = jnp.take_along_axis(fp, idx0, axis=-1) fp1 = jnp.take_along_axis(fp, idx1, axis=-1) vals.append((fp0, fp1))
FOR
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for<fim_suffix><fim_middle> item in fn(*args, **kwargs): results_queue.put(item)
item in fn(*args, **kwargs): results_queue.put(item)
FOR
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/geopoly.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Tools for constructing geodesic polyhedron, which are used as a basis.""" import itertools import numpy as np def compute_sq_dist(mat0, mat1=None): """Compute the squared Euclidean distance between all pairs of columns.""" if mat1 is None: mat1 = mat0 # Use the fact that ||x - y||^2 == ||x||^2 + ||y||^2 - 2 x^T y. sq_norm0 = np.sum(mat0**2, 0) sq_norm1 = np.sum(mat1**2, 0) sq_dist = sq_norm0[:, None] + sq_norm1[None, :] - 2 * mat0.T @ mat1 sq_dist = np.maximum(0, sq_dist) # Negative values must be numerical errors. return sq_dist def compute_tesselation_weights(v): """Tesselate the vertices of a triangle by a factor of `v`.""" if v < 1: raise ValueError(f'v {v} must be >= 1') int_weights = [] for i in range(v + 1): for j in range(v + 1 - i): int_weights.append((i, j, v - (i + j))) int_weights = np.array(int_weights) weights = int_weights / v # Barycentric weights. return weights def tesselate_geodesic(base_verts, base_faces, v, eps=1e-4): """Tesselate the vertices of a geodesic polyhedron. Args: base_verts: tensor of floats, the vertex coordinates of the geodesic. base_faces: tensor of ints, the indices of the vertices of base_verts that constitute eachface of the polyhedra. v: int, the factor of the tesselation (v==1 is a no-op). eps: float, a small value used to determine if two vertices are the same. Returns: verts: a tensor of floats, the coordinates of the tesselated vertices. """ if not isinstance(v, int): raise ValueError(f'v {v} must an integer') tri_weights = compute_tesselation_weights(v) verts = [] for<fim_suffix><fim_middle> base_face in base_faces: new_verts = np.matmul(tri_weights, base_verts[base_face, :]) new_verts /= np.sqrt(np.sum(new_verts**2, 1, keepdims=True)) verts.append(new_verts)
base_face in base_faces: new_verts = np.matmul(tri_weights, base_verts[base_face, :]) new_verts /= np.sqrt(np.sum(new_verts**2, 1, keepdims=True)) verts.append(new_verts)
FOR
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/ref_utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Functions for reflection directions and directional encodings.""" import math from internal import math as math_lib import jax.numpy as jnp import numpy as np def reflect(viewdirs, normals): """Reflect view directions about normals. The reflection of a vector v about a unit vector n is a vector u such that dot(v, n) = dot(u, n), and dot(u, u) = dot(v, v). The solution to these two equations is u = 2 dot(n, v) n - v. Args: viewdirs: [..., 3] array of view directions. normals: [..., 3] array of normal directions (assumed to be unit vectors). Returns: [..., 3] array of reflection directions. """ return ( 2.0 * jnp.sum(normals * viewdirs, axis=-1, keepdims=True) * normals - viewdirs ) def l2_normalize(x, grad_eps=jnp.finfo(jnp.float32).eps): """Normalize x to unit length along last axis. Normalizing vectors is surprisingly tricky, because you have to address the case where the denominator in the normalization is tiny or zero, in which case gradients will explode. For this reason, we perform two normalizations: in the forward pass, we clamp the denominator with ~1e-40, but in the backward pass we clamp with `grad_eps`, which defaults to ~1e-7. This guarantees that the output of this function is unit norm (unless x is very very small) while preventing exploding gradients. Args: x: The array of values to normalize. grad_eps: The value to clip the squared norm by before division in the backward pass. Returns: A normalized array x / ||x||, normalized along the last axis. """ tiny = jnp.finfo(jnp.float32).tiny grad_eps = jnp.maximum(tiny, grad_eps) denom_sq = jnp.sum(x**2, axis=-1, keepdims=True) normal_val = x / jnp.sqrt(jnp.maximum(tiny, denom_sq)) normal_grad = x / jnp.sqrt(jnp.maximum(grad_eps, denom_sq)) # Use `normal_val` in the forward pass but `normal_grad` in the backward pass. normal = math_lib.override_gradient(normal_val, normal_grad) return jnp.where(denom_sq < tiny, jnp.zeros_like(normal), normal) def compute_weighted_mae(weights, normals, normals_gt): """Compute weighted mean angular error, assuming normals are unit length.""" angles = math_lib.safe_arccos((normals * normals_gt).sum(axis=-1)) return (180.0 / jnp.pi) * ((weights * angles).sum() / weights.sum()) def generalized_binomial_coeff(a, k): """Compute generalized binomial coefficients.""" return np.prod(a - np.arange(k)) / math.factorial(k) def assoc_legendre_coeff(l, m, k): """Compute associated Legendre polynomial coefficients. Returns the coefficient of the cos^k(theta)*sin^m(theta) term in the (l, m)th associated Legendre polynomial, P_l^m(cos(theta)). Args: l: associated Legendre polynomial degree. m: associated Legendre polynomial order. k: power of cos(theta). Returns: A float, the coefficient of the term corresponding to the inputs. """ return ( (-1) ** m * 2**l * math.factorial(l) / math.factorial(k) / math.factorial(l - k - m) * generalized_binomial_coeff(0.5 * (l + k + m - 1.0), l) ) def sph_harm_coeff(l, m, k): """Compute spherical harmonic coefficients.""" return np.sqrt( (2.0 * l + 1.0) * math.factorial(l - m) / (4.0 * np.pi * math.factorial(l + m)) ) * assoc_legendre_coeff(l, m, k) def get_ml_array(deg_view): """Create a list with all pairs of (l, m) values to use in the encoding.""" ml_list = [] for i in range(deg_view): l = 2**i # Only use nonnegative m values, later splitting real and imaginary parts. for<fim_suffix><fim_middle> m in range(l + 1): ml_list.append((m, l))
m in range(l + 1): ml_list.append((m, l))
FOR
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for<fim_suffix><fim_middle> item in fn(*args, **kwargs): results_queue.put(item)
item in fn(*args, **kwargs): results_queue.put(item)
FOR
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def<fim_suffix><fim_middle> result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except queue.Empty: continue logging.info('Got data in %0.3fs', time.time() - get_start) yield next_value # Thread exception will be raised here thread_fn_future.result()
result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except queue.Empty: continue logging.info('Got data in %0.3fs', time.time() - get_start) yield next_value # Thread exception will be raised here thread_fn_future.result()
METHOD
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def<fim_suffix><fim_middle> decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except queue.Empty: continue logging.info('Got data in %0.3fs', time.time() - get_start) yield next_value # Thread exception will be raised here thread_fn_future.result() return result_fn
decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except queue.Empty: continue logging.info('Got data in %0.3fs', time.time() - get_start) yield next_value # Thread exception will be raised here thread_fn_future.result() return result_fn
METHOD
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/ref_utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Functions for reflection directions and directional encodings.""" import math from internal import math as math_lib import jax.numpy as jnp import numpy as np def reflect(viewdirs, normals): """Reflect view directions about normals. The reflection of a vector v about a unit vector n is a vector u such that dot(v, n) = dot(u, n), and dot(u, u) = dot(v, v). The solution to these two equations is u = 2 dot(n, v) n - v. Args: viewdirs: [..., 3] array of view directions. normals: [..., 3] array of normal directions (assumed to be unit vectors). Returns: [..., 3] array of reflection directions. """ return ( 2.0 * jnp.sum(normals * viewdirs, axis=-1, keepdims=True) * normals - viewdirs ) def l2_normalize(x, grad_eps=jnp.finfo(jnp.float32).eps): """Normalize x to unit length along last axis. Normalizing vectors is surprisingly tricky, because you have to address the case where the denominator in the normalization is tiny or zero, in which case gradients will explode. For this reason, we perform two normalizations: in the forward pass, we clamp the denominator with ~1e-40, but in the backward pass we clamp with `grad_eps`, which defaults to ~1e-7. This guarantees that the output of this function is unit norm (unless x is very very small) while preventing exploding gradients. Args: x: The array of values to normalize. grad_eps: The value to clip the squared norm by before division in the backward pass. Returns: A normalized array x / ||x||, normalized along the last axis. """ tiny = jnp.finfo(jnp.float32).tiny grad_eps = jnp.maximum(tiny, grad_eps) denom_sq = jnp.sum(x**2, axis=-1, keepdims=True) normal_val = x / jnp.sqrt(jnp.maximum(tiny, denom_sq)) normal_grad = x / jnp.sqrt(jnp.maximum(grad_eps, denom_sq)) # Use `normal_val` in the forward pass but `normal_grad` in the backward pass. normal = math_lib.override_gradient(normal_val, normal_grad) return jnp.where(denom_sq < tiny, jnp.zeros_like(normal), normal) def compute_weighted_mae(weights, normals, normals_gt): """Compute weighted mean angular error, assuming normals are unit length.""" angles = math_lib.safe_arccos((normals * normals_gt).sum(axis=-1)) return (180.0 / jnp.pi) * ((weights * angles).sum() / weights.sum()) def generalized_binomial_coeff(a, k): """Compute generalized binomial coefficients.""" return np.prod(a - np.arange(k)) / math.factorial(k) def assoc_legendre_coeff(l, m, k): """Compute associated Legendre polynomial coefficients. Returns the coefficient of the cos^k(theta)*sin^m(theta) term in the (l, m)th associated Legendre polynomial, P_l^m(cos(theta)). Args: l: associated Legendre polynomial degree. m: associated Legendre polynomial order. k: power of cos(theta). Returns: A float, the coefficient of the term corresponding to the inputs. """ return ( (-1) ** m * 2**l * math.factorial(l) / math.factorial(k) / math.factorial(l - k - m) * generalized_binomial_coeff(0.5 * (l + k + m - 1.0), l) ) def sph_harm_coeff(l, m, k): """Compute spherical harmonic coefficients.""" return np.sqrt( (2.0 * l + 1.0) * math.factorial(l - m) / (4.0 * np.pi * math.factorial(l + m)) ) * assoc_legendre_coeff(l, m, k) def get_ml_array(deg_view): """Create a list with all pairs of (l, m) values to use in the encoding.""" ml_list = [] for i in range(deg_view): l = 2**i # Only use nonnegative m values, later splitting real and imaginary parts. for m in range(l + 1): ml_list.append((m, l)) # Convert list into a numpy array. ml_array = np.array(ml_list).T return ml_array def generate_ide_fn(deg_view): """Generate integrated directional encoding (IDE) function. This function returns a function that computes the integrated directional encoding from Equations 6-8 of arxiv.org/abs/2112.03907. Args: deg_view: number of spherical harmonics degrees to use. Returns: A function for evaluating integrated directional encoding. Raises: ValueError: if deg_view is larger than 5. """ if deg_view > 5: raise ValueError('Only deg_view of at most 5 is numerically stable.') ml_array = get_ml_array(deg_view) l_max = 2 ** (deg_view - 1) # Create a matrix corresponding to ml_array holding all coefficients, which, # when multiplied (from the right) by the z coordinate Vandermonde matrix, # results in the z component of the encoding. mat = np.zeros((l_max + 1, ml_array.shape[1])) for i, (m, l) in enumerate(ml_array.T): for k in range(l - m + 1): mat[k, i] = sph_harm_coeff(l, m, k) def integrated_dir_enc_fn(xyz, kappa_inv): """Function returning integrated directional encoding (IDE). Args: xyz: [..., 3] array of Cartesian coordinates of directions to evaluate at. kappa_inv: [..., 1] reciprocal of the concentration parameter of the von Mises-Fisher distribution. Returns: An array with the resulting IDE. """ x = xyz[Ellipsis, 0:1] y = xyz[Ellipsis, 1:2] z = xyz[Ellipsis, 2:3] # Compute z Vandermonde matrix. vmz = jnp.concatenate([z**i for i in range(mat.shape[0])], axis=-1) # Compute x+iy Vandermonde matrix. vmxy = jnp.concatenate([(x + 1j * y) ** m for m in ml_array[0, :]], axis=-1) # Get spherical harmonics. sph_harms = vmxy * math_lib.matmul(vmz, mat) # Apply attenuation function using the von Mises-Fisher distribution # concentration parameter, kappa. sigma = 0.5 * ml_array[1, :] * (ml_array[1, :] + 1) ide = sph_harms * jnp.exp(-sigma * kappa_inv) # Split into real and imaginary parts and return return jnp.concatenate([jnp.real(ide), jnp.imag(ide)], axis=-1) return integrated_dir_enc_fn def generate_dir_enc_fn(deg_view): """Generate directional encoding (DE) function. Args: deg_view: number of spherical harmonics degrees to use. Returns: A function for evaluating directional encoding. """ integrated_dir_enc_fn = generate_ide_fn(deg_view) def<fim_suffix><fim_middle> dir_enc_fn(xyz): """Function returning directional encoding (DE).""" return integrated_dir_enc_fn(xyz, jnp.zeros_like(xyz[Ellipsis, :1]))
dir_enc_fn(xyz): """Function returning directional encoding (DE).""" return integrated_dir_enc_fn(xyz, jnp.zeros_like(xyz[Ellipsis, :1]))
METHOD
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/ref_utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Functions for reflection directions and directional encodings.""" import math from internal import math as math_lib import jax.numpy as jnp import numpy as np def reflect(viewdirs, normals): """Reflect view directions about normals. The reflection of a vector v about a unit vector n is a vector u such that dot(v, n) = dot(u, n), and dot(u, u) = dot(v, v). The solution to these two equations is u = 2 dot(n, v) n - v. Args: viewdirs: [..., 3] array of view directions. normals: [..., 3] array of normal directions (assumed to be unit vectors). Returns: [..., 3] array of reflection directions. """ return ( 2.0 * jnp.sum(normals * viewdirs, axis=-1, keepdims=True) * normals - viewdirs ) def l2_normalize(x, grad_eps=jnp.finfo(jnp.float32).eps): """Normalize x to unit length along last axis. Normalizing vectors is surprisingly tricky, because you have to address the case where the denominator in the normalization is tiny or zero, in which case gradients will explode. For this reason, we perform two normalizations: in the forward pass, we clamp the denominator with ~1e-40, but in the backward pass we clamp with `grad_eps`, which defaults to ~1e-7. This guarantees that the output of this function is unit norm (unless x is very very small) while preventing exploding gradients. Args: x: The array of values to normalize. grad_eps: The value to clip the squared norm by before division in the backward pass. Returns: A normalized array x / ||x||, normalized along the last axis. """ tiny = jnp.finfo(jnp.float32).tiny grad_eps = jnp.maximum(tiny, grad_eps) denom_sq = jnp.sum(x**2, axis=-1, keepdims=True) normal_val = x / jnp.sqrt(jnp.maximum(tiny, denom_sq)) normal_grad = x / jnp.sqrt(jnp.maximum(grad_eps, denom_sq)) # Use `normal_val` in the forward pass but `normal_grad` in the backward pass. normal = math_lib.override_gradient(normal_val, normal_grad) return jnp.where(denom_sq < tiny, jnp.zeros_like(normal), normal) def compute_weighted_mae(weights, normals, normals_gt): """Compute weighted mean angular error, assuming normals are unit length.""" angles = math_lib.safe_arccos((normals * normals_gt).sum(axis=-1)) return (180.0 / jnp.pi) * ((weights * angles).sum() / weights.sum()) def generalized_binomial_coeff(a, k): """Compute generalized binomial coefficients.""" return np.prod(a - np.arange(k)) / math.factorial(k) def assoc_legendre_coeff(l, m, k): """Compute associated Legendre polynomial coefficients. Returns the coefficient of the cos^k(theta)*sin^m(theta) term in the (l, m)th associated Legendre polynomial, P_l^m(cos(theta)). Args: l: associated Legendre polynomial degree. m: associated Legendre polynomial order. k: power of cos(theta). Returns: A float, the coefficient of the term corresponding to the inputs. """ return ( (-1) ** m * 2**l * math.factorial(l) / math.factorial(k) / math.factorial(l - k - m) * generalized_binomial_coeff(0.5 * (l + k + m - 1.0), l) ) def sph_harm_coeff(l, m, k): """Compute spherical harmonic coefficients.""" return np.sqrt( (2.0 * l + 1.0) * math.factorial(l - m) / (4.0 * np.pi * math.factorial(l + m)) ) * assoc_legendre_coeff(l, m, k) def get_ml_array(deg_view): """Create a list with all pairs of (l, m) values to use in the encoding.""" ml_list = [] for i in range(deg_view): l = 2**i # Only use nonnegative m values, later splitting real and imaginary parts. for m in range(l + 1): ml_list.append((m, l)) # Convert list into a numpy array. ml_array = np.array(ml_list).T return ml_array def generate_ide_fn(deg_view): """Generate integrated directional encoding (IDE) function. This function returns a function that computes the integrated directional encoding from Equations 6-8 of arxiv.org/abs/2112.03907. Args: deg_view: number of spherical harmonics degrees to use. Returns: A function for evaluating integrated directional encoding. Raises: ValueError: if deg_view is larger than 5. """ if deg_view > 5: raise ValueError('Only deg_view of at most 5 is numerically stable.') ml_array = get_ml_array(deg_view) l_max = 2 ** (deg_view - 1) # Create a matrix corresponding to ml_array holding all coefficients, which, # when multiplied (from the right) by the z coordinate Vandermonde matrix, # results in the z component of the encoding. mat = np.zeros((l_max + 1, ml_array.shape[1])) for i, (m, l) in enumerate(ml_array.T): for k in range(l - m + 1): mat[k, i] = sph_harm_coeff(l, m, k) def<fim_suffix><fim_middle> integrated_dir_enc_fn(xyz, kappa_inv): """Function returning integrated directional encoding (IDE). Args: xyz: [..., 3] array of Cartesian coordinates of directions to evaluate at. kappa_inv: [..., 1] reciprocal of the concentration parameter of the von Mises-Fisher distribution. Returns: An array with the resulting IDE. """ x = xyz[Ellipsis, 0:1] y = xyz[Ellipsis, 1:2] z = xyz[Ellipsis, 2:3] # Compute z Vandermonde matrix. vmz = jnp.concatenate([z**i for i in range(mat.shape[0])], axis=-1) # Compute x+iy Vandermonde matrix. vmxy = jnp.concatenate([(x + 1j * y) ** m for m in ml_array[0, :]], axis=-1) # Get spherical harmonics. sph_harms = vmxy * math_lib.matmul(vmz, mat) # Apply attenuation function using the von Mises-Fisher distribution # concentration parameter, kappa. sigma = 0.5 * ml_array[1, :] * (ml_array[1, :] + 1) ide = sph_harms * jnp.exp(-sigma * kappa_inv) # Split into real and imaginary parts and return return jnp.concatenate([jnp.real(ide), jnp.imag(ide)], axis=-1)
integrated_dir_enc_fn(xyz, kappa_inv): """Function returning integrated directional encoding (IDE). Args: xyz: [..., 3] array of Cartesian coordinates of directions to evaluate at. kappa_inv: [..., 1] reciprocal of the concentration parameter of the von Mises-Fisher distribution. Returns: An array with the resulting IDE. """ x = xyz[Ellipsis, 0:1] y = xyz[Ellipsis, 1:2] z = xyz[Ellipsis, 2:3] # Compute z Vandermonde matrix. vmz = jnp.concatenate([z**i for i in range(mat.shape[0])], axis=-1) # Compute x+iy Vandermonde matrix. vmxy = jnp.concatenate([(x + 1j * y) ** m for m in ml_array[0, :]], axis=-1) # Get spherical harmonics. sph_harms = vmxy * math_lib.matmul(vmz, mat) # Apply attenuation function using the von Mises-Fisher distribution # concentration parameter, kappa. sigma = 0.5 * ml_array[1, :] * (ml_array[1, :] + 1) ide = sph_harms * jnp.exp(-sigma * kappa_inv) # Split into real and imaginary parts and return return jnp.concatenate([jnp.real(ide), jnp.imag(ide)], axis=-1)
METHOD
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def<fim_suffix><fim_middle> thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False
thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False
METHOD
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/math.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Mathy utility functions.""" import functools import jax import jax.numpy as jnp import numpy as np tiny_val = np.float32(np.finfo(np.float32).tiny) min_val = np.float32(np.finfo(np.float32).min) max_val = np.float32(np.finfo(np.float32).max) def laplace_cdf(x, beta): alpha = 1 / beta return alpha * (0.5 + 0.5 * safe_sign(x) * (jnp.exp(-jnp.abs(x) / beta) - 1)) def scaled_softplus(x, scale=100.0): return (1.0 / scale) * jax.nn.softplus(scale * x) def matmul(a, b): """jnp.matmul defaults to bfloat16, but this helper function doesn't.""" return jnp.matmul(a, b, precision=jax.lax.Precision.HIGHEST) def unstack(x, axis=0): return tuple( jnp.squeeze(z, axis=axis) for z in jnp.split(x, x.shape[axis], axis=axis) ) @jax.custom_jvp def plus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, tiny_val, jnp.nextafter(jnp.float32(x), jnp.inf) ) @jax.custom_jvp def minus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, -tiny_val, jnp.nextafter(jnp.float32(x), -jnp.inf) ) @plus_eps.defjvp def plus_eps_jvp(primals, tangents): """Make plus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return plus_eps(*primals), tangents[0] @minus_eps.defjvp def minus_eps_jvp(primals, tangents): """Make minus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return minus_eps(*primals), tangents[0] @jax.custom_jvp def expm1(x): """jnp.expm1() has inaccurate gradients when x << 0, this doesn't.""" return jnp.expm1(x) @expm1.defjvp def expm1_jvp(primals, tangents): return expm1(*primals), tangents[0] * jnp.exp(primals[0]) def safe_trig_helper(x, fn, t=100 * jnp.pi): """Helper function used by safe_cos/safe_sin: mods x before sin()/cos().""" return fn(jnp.nan_to_num(jnp.where(jnp.abs(x) < t, x, x % t))) def safe_cos(x): """jnp.cos() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.cos) def safe_sin(x): """jnp.sin() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.sin) @jax.custom_vjp def safe_arctan2(x1, x2): return safe_arctan2_fwd(x1, x2)[0] def safe_arctan2_fwd(x1, x2): return jnp.arctan2(x1, x2), (x1, x2) def safe_arctan2_bwd(res, g): x1, x2 = res denom = remove_zero(x1**2 + x2**2) d1 = g * (x2 / denom) d2 = g * (-x1 / denom) return d1, d2 safe_arctan2.defvjp(safe_arctan2_fwd, safe_arctan2_bwd) def generate_clip_nograd_fn(a_min, a_max): """Generates a function that clips to [a_min, a_max] with no grad effects.""" @jax.custom_jvp def clip_nograd(a): """Clamps `a` from above and below.""" return jnp.clip(a, a_min, a_max) @clip_nograd.defjvp def clip_nograd_jvp(primals, tangents): """Override clips()'s gradient to be a no-op.""" return clip_nograd(primals[0]), tangents[0] return clip_nograd clip_finite_nograd = generate_clip_nograd_fn(min_val, max_val) clip_pos_finite_nograd = generate_clip_nograd_fn(tiny_val, max_val) def clip_pos(x): """Clamps `x` from below to be positive.""" return jnp.maximum(tiny_val, x) def safe_sign(x): """jnp.sign(x) except x=0 is assumed to have a sign of +1, not 0.""" return jnp.where(x < 0, -1, +1) def remove_zero(x): """Shifts `x` away from 0.""" return jnp.where(jnp.abs(x) < tiny_val, tiny_val, x) def clip_finite(x): return jnp.clip(x, min_val, max_val) @jax.custom_vjp def safe_div(n, d): """Divide `n` by `d` but the value and gradient never nan out.""" return safe_div_fwd(n, d)[0] def safe_div_fwd(n, d): r = jnp.clip(n / remove_zero(d), min_val, max_val) return jnp.where(jnp.abs(d) < tiny_val, 0, r), (d, r) def safe_div_bwd(res, g): d, r = res dn = jnp.clip(g / remove_zero(d), min_val, max_val) dd = jnp.clip(-g * r / remove_zero(d), min_val, max_val) return dn, dd safe_div.defvjp(safe_div_fwd, safe_div_bwd) def generate_safe_fn(fn, grad_fn, x_range): """Generate's a `safe` fn() where inputs are clipped in fwd and bwd passes.""" @jax.custom_jvp def<fim_suffix><fim_middle> safe_fn(x): """fn() with clipped inputs.""" return fn(jnp.clip(x, *x_range))
safe_fn(x): """fn() with clipped inputs.""" return fn(jnp.clip(x, *x_range))
METHOD
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def<fim_suffix><fim_middle> thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False
thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False
METHOD
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def<fim_suffix><fim_middle> thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False
thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False
METHOD
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/math.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Mathy utility functions.""" import functools import jax import jax.numpy as jnp import numpy as np tiny_val = np.float32(np.finfo(np.float32).tiny) min_val = np.float32(np.finfo(np.float32).min) max_val = np.float32(np.finfo(np.float32).max) def laplace_cdf(x, beta): alpha = 1 / beta return alpha * (0.5 + 0.5 * safe_sign(x) * (jnp.exp(-jnp.abs(x) / beta) - 1)) def scaled_softplus(x, scale=100.0): return (1.0 / scale) * jax.nn.softplus(scale * x) def matmul(a, b): """jnp.matmul defaults to bfloat16, but this helper function doesn't.""" return jnp.matmul(a, b, precision=jax.lax.Precision.HIGHEST) def unstack(x, axis=0): return tuple( jnp.squeeze(z, axis=axis) for z in jnp.split(x, x.shape[axis], axis=axis) ) @jax.custom_jvp def plus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, tiny_val, jnp.nextafter(jnp.float32(x), jnp.inf) ) @jax.custom_jvp def minus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, -tiny_val, jnp.nextafter(jnp.float32(x), -jnp.inf) ) @plus_eps.defjvp def plus_eps_jvp(primals, tangents): """Make plus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return plus_eps(*primals), tangents[0] @minus_eps.defjvp def minus_eps_jvp(primals, tangents): """Make minus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return minus_eps(*primals), tangents[0] @jax.custom_jvp def expm1(x): """jnp.expm1() has inaccurate gradients when x << 0, this doesn't.""" return jnp.expm1(x) @expm1.defjvp def expm1_jvp(primals, tangents): return expm1(*primals), tangents[0] * jnp.exp(primals[0]) def safe_trig_helper(x, fn, t=100 * jnp.pi): """Helper function used by safe_cos/safe_sin: mods x before sin()/cos().""" return fn(jnp.nan_to_num(jnp.where(jnp.abs(x) < t, x, x % t))) def safe_cos(x): """jnp.cos() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.cos) def safe_sin(x): """jnp.sin() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.sin) @jax.custom_vjp def safe_arctan2(x1, x2): return safe_arctan2_fwd(x1, x2)[0] def safe_arctan2_fwd(x1, x2): return jnp.arctan2(x1, x2), (x1, x2) def safe_arctan2_bwd(res, g): x1, x2 = res denom = remove_zero(x1**2 + x2**2) d1 = g * (x2 / denom) d2 = g * (-x1 / denom) return d1, d2 safe_arctan2.defvjp(safe_arctan2_fwd, safe_arctan2_bwd) def generate_clip_nograd_fn(a_min, a_max): """Generates a function that clips to [a_min, a_max] with no grad effects.""" @jax.custom_jvp def clip_nograd(a): """Clamps `a` from above and below.""" return jnp.clip(a, a_min, a_max) @clip_nograd.defjvp def clip_nograd_jvp(primals, tangents): """Override clips()'s gradient to be a no-op.""" return clip_nograd(primals[0]), tangents[0] return clip_nograd clip_finite_nograd = generate_clip_nograd_fn(min_val, max_val) clip_pos_finite_nograd = generate_clip_nograd_fn(tiny_val, max_val) def clip_pos(x): """Clamps `x` from below to be positive.""" return jnp.maximum(tiny_val, x) def safe_sign(x): """jnp.sign(x) except x=0 is assumed to have a sign of +1, not 0.""" return jnp.where(x < 0, -1, +1) def remove_zero(x): """Shifts `x` away from 0.""" return jnp.where(jnp.abs(x) < tiny_val, tiny_val, x) def clip_finite(x): return jnp.clip(x, min_val, max_val) @jax.custom_vjp def safe_div(n, d): """Divide `n` by `d` but the value and gradient never nan out.""" return safe_div_fwd(n, d)[0] def safe_div_fwd(n, d): r = jnp.clip(n / remove_zero(d), min_val, max_val) return jnp.where(jnp.abs(d) < tiny_val, 0, r), (d, r) def safe_div_bwd(res, g): d, r = res dn = jnp.clip(g / remove_zero(d), min_val, max_val) dd = jnp.clip(-g * r / remove_zero(d), min_val, max_val) return dn, dd safe_div.defvjp(safe_div_fwd, safe_div_bwd) def generate_safe_fn(fn, grad_fn, x_range): """Generate's a `safe` fn() where inputs are clipped in fwd and bwd passes.""" @jax.custom_jvp def safe_fn(x): """fn() with clipped inputs.""" return fn(jnp.clip(x, *x_range)) @safe_fn.defjvp def<fim_suffix><fim_middle> safe_fn_jvp(primals, tangents): """Backpropagate using the gradient and clipped inputs.""" (x,) = primals (x_dot,) = tangents y = safe_fn(x) y_dot = grad_fn(jnp.clip(x, *x_range), y, x_dot) return y, y_dot
safe_fn_jvp(primals, tangents): """Backpropagate using the gradient and clipped inputs.""" (x,) = primals (x_dot,) = tangents y = safe_fn(x) y_dot = grad_fn(jnp.clip(x, *x_range), y, x_dot) return y, y_dot
METHOD
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def<fim_suffix><fim_middle> result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except queue.Empty: continue logging.info('Got data in %0.3fs', time.time() - get_start) yield next_value # Thread exception will be raised here thread_fn_future.result()
result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except queue.Empty: continue logging.info('Got data in %0.3fs', time.time() - get_start) yield next_value # Thread exception will be raised here thread_fn_future.result()
METHOD
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try:<fim_suffix><fim_middle> for item in fn(*args, **kwargs): results_queue.put(item)
for item in fn(*args, **kwargs): results_queue.put(item)
TRY
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try:<fim_suffix><fim_middle> # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0)
# Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0)
TRY
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try:<fim_suffix><fim_middle> # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0)
# Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0)
TRY
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try:<fim_suffix><fim_middle> for item in fn(*args, **kwargs): results_queue.put(item)
for item in fn(*args, **kwargs): results_queue.put(item)
TRY
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try:<fim_suffix><fim_middle> # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0)
# Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0)
TRY
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try:<fim_suffix><fim_middle> for item in fn(*args, **kwargs): results_queue.put(item)
for item in fn(*args, **kwargs): results_queue.put(item)
TRY
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while<fim_suffix><fim_middle> True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except queue.Empty: continue logging.info('Got data in %0.3fs', time.time() - get_start) yield next_value
True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except queue.Empty: continue logging.info('Got data in %0.3fs', time.time() - get_start) yield next_value
WHILE
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while<fim_suffix><fim_middle> True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except queue.Empty: continue logging.info('Got data in %0.3fs', time.time() - get_start) yield next_value
True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except queue.Empty: continue logging.info('Got data in %0.3fs', time.time() - get_start) yield next_value
WHILE
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while<fim_suffix><fim_middle> True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except queue.Empty: continue logging.info('Got data in %0.3fs', time.time() - get_start) yield next_value
True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except queue.Empty: continue logging.info('Got data in %0.3fs', time.time() - get_start) yield next_value
WHILE
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except<fim_suffix><fim_middle> queue.Empty: continue
queue.Empty: continue
CATCH
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except<fim_suffix><fim_middle> queue.Empty: continue
queue.Empty: continue
CATCH
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/utils.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Utility functions.""" import concurrent import enum import os import queue import threading import time from typing import Any, Callable, Iterable, Optional, TypeVar, Union from absl import logging import flax import jax from jax import random import jax.numpy as jnp import numpy as np _Array = Union[np.ndarray, jnp.ndarray] @flax.struct.dataclass class Rays: """All tensors must have the same num_dims and first n-1 dims must match. This dataclass contains spatially meaningful quantities associated with the ray that can be calculated by the function casting the ray, as well as all metadata necessary for the ray to be rendered by the Model class. """ origins: Optional[_Array] = None directions: Optional[_Array] = None viewdirs: Optional[_Array] = None radii: Optional[_Array] = None imageplane: Optional[_Array] = None pixels: Optional[_Array] = None lossmult: Optional[_Array] = None near: Optional[_Array] = None far: Optional[_Array] = None cam_idx: Optional[_Array] = None exposure_idx: Optional[_Array] = None exposure_values: Optional[_Array] = None device_idx: Optional[_Array] = None def generate_random_rays( rng, n, origin_lo, origin_hi, radius_lo, radius_hi, near_lo, near_hi, far_lo, far_hi, include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): """Generate a random Rays datastructure.""" key, rng = random.split(rng) origins = random.uniform( key, shape=[n, 3], minval=origin_lo, maxval=origin_hi ) key, rng = random.split(rng) directions = random.normal(key, shape=[n, 3]) directions /= jnp.sqrt( jnp.maximum( jnp.finfo(jnp.float32).tiny, jnp.sum(directions**2, axis=-1, keepdims=True), ) ) viewdirs = directions key, rng = random.split(rng) radii = random.uniform(key, shape=[n, 1], minval=radius_lo, maxval=radius_hi) key, rng = random.split(rng) near = random.uniform(key, shape=[n, 1], minval=near_lo, maxval=near_hi) key, rng = random.split(rng) far = random.uniform(key, shape=[n, 1], minval=far_lo, maxval=far_hi) imageplane = jnp.zeros([n, 2]) lossmult = jnp.zeros([n, 1]) key, rng = random.split(rng) pixels = random.randint(key, shape=[n, 2], minval=0, maxval=1024) int_scalar = jnp.int32(jnp.zeros([n, 1])) exposure_kwargs = {} if include_exposure_idx: exposure_kwargs['exposure_idx'] = int_scalar if include_exposure_values: exposure_kwargs['exposure_values'] = jnp.zeros([n, 1]) if include_device_idx: exposure_kwargs['device_idx'] = int_scalar random_rays = Rays( origins=origins, directions=directions, viewdirs=viewdirs, radii=radii, imageplane=imageplane, pixels=pixels, lossmult=lossmult, near=near, far=far, cam_idx=int_scalar, **exposure_kwargs, ) return random_rays # Dummy Rays object that can be used to initialize NeRF model. def dummy_rays( include_exposure_idx = False, include_exposure_values = False, include_device_idx = False, ): return generate_random_rays( random.PRNGKey(0), n=100, origin_lo=-1.5, origin_hi=1.5, radius_lo=1e-5, radius_hi=1e-3, near_lo=0.0, near_hi=1.0, far_lo=10, far_hi=10000, include_exposure_idx=include_exposure_idx, include_exposure_values=include_exposure_values, include_device_idx=include_device_idx, ) @flax.struct.dataclass class Batch: """Data batch for NeRF training or testing. This dataclass contains rays and also per-pixel data that is necessary for computing the loss term or evaluating metrics but NOT necessary for rendering. """ rays: Rays rgb: Optional[_Array] = None disps: Optional[_Array] = None normals: Optional[_Array] = None alphas: Optional[_Array] = None masks: Optional[_Array] = None class DataSplit(enum.Enum): """Dataset split.""" TRAIN = 'train' TEST = 'test' class BatchingMethod(enum.Enum): """Draw rays randomly from a single image or all images, in each batch.""" ALL_IMAGES = 'all_images' SINGLE_IMAGE = 'single_image' def open_file(pth, mode='r'): return open(pth, mode=mode) def file_exists(pth): return os.path.exists(pth) def listdir(pth): return os.listdir(pth) def isdir(pth): return os.path.isdir(pth) def makedirs(pth): if not file_exists(pth): os.makedirs(pth) def device_is_tpu(): return jax.local_devices()[0].platform == 'tpu' def shard(xs): """Split data into shards for multiple devices along the first dimension.""" return jax.tree_util.tree_map( lambda x: x.reshape((jax.local_device_count(), -1) + x.shape[1:]), xs ) def unshard(x, padding=0): """Collect the sharded tensor to the shape before sharding.""" y = x.reshape([x.shape[0] * x.shape[1]] + list(x.shape[2:])) if padding > 0: y = y[:-padding] return y def load_npy(pth): """Load an numpy array cast to float32.""" with open_file(pth, 'rb') as f: x = np.load(f).astype(np.float32) return x def assert_valid_stepfun(t, y): """Assert that step function (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1] + 1: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a step function.' ) def assert_valid_linspline(t, y): """Assert that piecewise linear spline (t, y) has a valid shape.""" if t.shape[-1] != y.shape[-1]: raise ValueError( f'Invalid shapes ({t.shape}, {y.shape}) for a linear spline.' ) _FnT = TypeVar('_FnT', bound=Callable[Ellipsis, Iterable[Any]]) def iterate_in_separate_thread( queue_size = 3, ): """Decorator factory that iterates a function in a separate thread. Args: queue_size: Keep at most queue_size elements in memory. Returns: Decorator that will iterate a function in a separate thread. """ def decorator( fn, ): def result_fn(*args, **kwargs): results_queue = queue.Queue(queue_size) populating_data = True populating_data_lock = threading.Lock() def thread_fn(): # Mark has_data as a variable that's outside of thread_fn # Otherwise, `populating_data = True` creates a local variable nonlocal populating_data try: for item in fn(*args, **kwargs): results_queue.put(item) finally: # Set populating_data to False regardless of exceptions to stop # iterations with populating_data_lock: populating_data = False # Use executor + futures instead of Thread to propagate exceptions with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor: thread_fn_future = executor.submit(thread_fn) while True: with populating_data_lock: if not populating_data and results_queue.empty(): break get_start = time.time() try: # Set timeout to allow for exceptions to be propagated. next_value = results_queue.get(timeout=1.0) except<fim_suffix><fim_middle> queue.Empty: continue
queue.Empty: continue
CATCH
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/math.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Mathy utility functions.""" import functools import jax import jax.numpy as jnp import numpy as np tiny_val = np.float32(np.finfo(np.float32).tiny) min_val = np.float32(np.finfo(np.float32).min) max_val = np.float32(np.finfo(np.float32).max) def laplace_cdf(x, beta): alpha = 1 / beta return alpha * (0.5 + 0.5 * safe_sign(x) * (jnp.exp(-jnp.abs(x) / beta) - 1)) def scaled_softplus(x, scale=100.0): return (1.0 / scale) * jax.nn.softplus(scale * x) def matmul(a, b): """jnp.matmul defaults to bfloat16, but this helper function doesn't.""" return jnp.matmul(a, b, precision=jax.lax.Precision.HIGHEST) def unstack(x, axis=0): return tuple( jnp.squeeze(z, axis=axis) for z in jnp.split(x, x.shape[axis], axis=axis) ) @jax.custom_jvp def plus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, tiny_val, jnp.nextafter(jnp.float32(x), jnp.inf) ) @jax.custom_jvp def minus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, -tiny_val, jnp.nextafter(jnp.float32(x), -jnp.inf) ) @plus_eps.defjvp def plus_eps_jvp(primals, tangents): """Make plus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return plus_eps(*primals), tangents[0] @minus_eps.defjvp def minus_eps_jvp(primals, tangents): """Make minus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return minus_eps(*primals), tangents[0] @jax.custom_jvp def expm1(x): """jnp.expm1() has inaccurate gradients when x << 0, this doesn't.""" return jnp.expm1(x) @expm1.defjvp def expm1_jvp(primals, tangents): return expm1(*primals), tangents[0] * jnp.exp(primals[0]) def safe_trig_helper(x, fn, t=100 * jnp.pi): """Helper function used by safe_cos/safe_sin: mods x before sin()/cos().""" return fn(jnp.nan_to_num(jnp.where(jnp.abs(x) < t, x, x % t))) def safe_cos(x): """jnp.cos() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.cos) def safe_sin(x): """jnp.sin() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.sin) @jax.custom_vjp def safe_arctan2(x1, x2): return safe_arctan2_fwd(x1, x2)[0] def safe_arctan2_fwd(x1, x2): return jnp.arctan2(x1, x2), (x1, x2) def safe_arctan2_bwd(res, g): x1, x2 = res denom = remove_zero(x1**2 + x2**2) d1 = g * (x2 / denom) d2 = g * (-x1 / denom) return d1, d2 safe_arctan2.defvjp(safe_arctan2_fwd, safe_arctan2_bwd) def generate_clip_nograd_fn(a_min, a_max): """Generates a function that clips to [a_min, a_max] with no grad effects.""" @jax.custom_jvp def clip_nograd(a): """Clamps `a` from above and below.""" return jnp.clip(a, a_min, a_max) @clip_nograd.defjvp def clip_nograd_jvp(primals, tangents): """Override clips()'s gradient to be a no-op.""" return clip_nograd(primals[0]), tangents[0] return clip_nograd clip_finite_nograd = generate_clip_nograd_fn(min_val, max_val) clip_pos_finite_nograd = generate_clip_nograd_fn(tiny_val, max_val) def clip_pos(x): """Clamps `x` from below to be positive.""" return jnp.maximum(tiny_val, x) def safe_sign(x): """jnp.sign(x) except x=0 is assumed to have a sign of +1, not 0.""" return jnp.where(x < 0, -1, +1) def remove_zero(x): """Shifts `x` away from 0.""" return jnp.where(jnp.abs(x) < tiny_val, tiny_val, x) def clip_finite(x): return jnp.clip(x, min_val, max_val) @jax.custom_vjp def safe_div(n, d): """Divide `n` by `d` but the value and gradient never nan out.""" return safe_div_fwd(n, d)[0] def safe_div_fwd(n, d): r = jnp.clip(n / remove_zero(d), min_val, max_val) return jnp.where(jnp.abs(d) < tiny_val, 0, r), (d, r) def safe_div_bwd(res, g): d, r = res dn = jnp.clip(g / remove_zero(d), min_val, max_val) dd = jnp.clip(-g * r / remove_zero(d), min_val, max_val) return dn, dd safe_div.defvjp(safe_div_fwd, safe_div_bwd) def generate_safe_fn(fn, grad_fn, x_range): """Generate's a `safe` fn() where inputs are clipped in fwd and bwd passes.""" @jax.custom_jvp<fim_suffix><fim_middle>
null
ANNOTATION
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>camp_zipnerf/internal/math.py<fim_prefix># coding=utf-8 # Copyright 2023 The Google Research Authors. # # 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 # # http://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. """Mathy utility functions.""" import functools import jax import jax.numpy as jnp import numpy as np tiny_val = np.float32(np.finfo(np.float32).tiny) min_val = np.float32(np.finfo(np.float32).min) max_val = np.float32(np.finfo(np.float32).max) def laplace_cdf(x, beta): alpha = 1 / beta return alpha * (0.5 + 0.5 * safe_sign(x) * (jnp.exp(-jnp.abs(x) / beta) - 1)) def scaled_softplus(x, scale=100.0): return (1.0 / scale) * jax.nn.softplus(scale * x) def matmul(a, b): """jnp.matmul defaults to bfloat16, but this helper function doesn't.""" return jnp.matmul(a, b, precision=jax.lax.Precision.HIGHEST) def unstack(x, axis=0): return tuple( jnp.squeeze(z, axis=axis) for z in jnp.split(x, x.shape[axis], axis=axis) ) @jax.custom_jvp def plus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, tiny_val, jnp.nextafter(jnp.float32(x), jnp.inf) ) @jax.custom_jvp def minus_eps(x): return jnp.where( jnp.abs(x) < tiny_val, -tiny_val, jnp.nextafter(jnp.float32(x), -jnp.inf) ) @plus_eps.defjvp def plus_eps_jvp(primals, tangents): """Make plus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return plus_eps(*primals), tangents[0] @minus_eps.defjvp def minus_eps_jvp(primals, tangents): """Make minus_eps()'s gradient a no-op (nextafter's gradient is undefined).""" return minus_eps(*primals), tangents[0] @jax.custom_jvp def expm1(x): """jnp.expm1() has inaccurate gradients when x << 0, this doesn't.""" return jnp.expm1(x) @expm1.defjvp def expm1_jvp(primals, tangents): return expm1(*primals), tangents[0] * jnp.exp(primals[0]) def safe_trig_helper(x, fn, t=100 * jnp.pi): """Helper function used by safe_cos/safe_sin: mods x before sin()/cos().""" return fn(jnp.nan_to_num(jnp.where(jnp.abs(x) < t, x, x % t))) def safe_cos(x): """jnp.cos() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.cos) def safe_sin(x): """jnp.sin() on a TPU may NaN out for large values.""" return safe_trig_helper(x, jnp.sin) @jax.custom_vjp def safe_arctan2(x1, x2): return safe_arctan2_fwd(x1, x2)[0] def safe_arctan2_fwd(x1, x2): return jnp.arctan2(x1, x2), (x1, x2) def safe_arctan2_bwd(res, g): x1, x2 = res denom = remove_zero(x1**2 + x2**2) d1 = g * (x2 / denom) d2 = g * (-x1 / denom) return d1, d2 safe_arctan2.defvjp(safe_arctan2_fwd, safe_arctan2_bwd) def generate_clip_nograd_fn(a_min, a_max): """Generates a function that clips to [a_min, a_max] with no grad effects.""" @jax.custom_jvp def clip_nograd(a): """Clamps `a` from above and below.""" return jnp.clip(a, a_min, a_max) @clip_nograd.defjvp def clip_nograd_jvp(primals, tangents): """Override clips()'s gradient to be a no-op.""" return clip_nograd(primals[0]), tangents[0] return clip_nograd clip_finite_nograd = generate_clip_nograd_fn(min_val, max_val) clip_pos_finite_nograd = generate_clip_nograd_fn(tiny_val, max_val) def clip_pos(x): """Clamps `x` from below to be positive.""" return jnp.maximum(tiny_val, x) def safe_sign(x): """jnp.sign(x) except x=0 is assumed to have a sign of +1, not 0.""" return jnp.where(x < 0, -1, +1) def remove_zero(x): """Shifts `x` away from 0.""" return jnp.where(jnp.abs(x) < tiny_val, tiny_val, x) def clip_finite(x): return jnp.clip(x, min_val, max_val) @jax.custom_vjp def safe_div(n, d): """Divide `n` by `d` but the value and gradient never nan out.""" return safe_div_fwd(n, d)[0] def safe_div_fwd(n, d): r = jnp.clip(n / remove_zero(d), min_val, max_val) return jnp.where(jnp.abs(d) < tiny_val, 0, r), (d, r) def safe_div_bwd(res, g): d, r = res dn = jnp.clip(g / remove_zero(d), min_val, max_val) dd = jnp.clip(-g * r / remove_zero(d), min_val, max_val) return dn, dd safe_div.defvjp(safe_div_fwd, safe_div_bwd) def generate_safe_fn(fn, grad_fn, x_range): """Generate's a `safe` fn() where inputs are clipped in fwd and bwd passes.""" @jax.custom_jvp def safe_fn(x): """fn() with clipped inputs.""" return fn(jnp.clip(x, *x_range)) @safe_fn.defjvp<fim_suffix><fim_middle>
null
ANNOTATION
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/agent_persistence_manager.py<fim_prefix>from agents.agent_serializer import AgentSerializer from integrations.memoize import memoize_to_sqlite from integrations.sqlite_agent_persistence import SQLiteAgentPersistence class AgentPersistenceManager: def __init__(self, db_filename="agents.db"): self.persistence = SQLiteAgentPersistence(db_filename) def remove_agent(self, agent): """ Remove an agent from the database. """ self.persistence.remove_agent(agent.id) def save_agent(self, agent): """ Serialize and save the agent state if it is a working agent and not a prime agent. """ if agent.is_working_agent() and not agent.is_prime_agent(): serialized_agent = AgentSerializer.serialize(agent) self.persistence.save_agent(serialized_agent) def load_agent(self, purpose, agent_lifecycle, openai_wrapper): """ Load an agent with the given purpose from the database. """ serialized_agent = self.persistence.fetch_agent(purpose) if serialized_agent: return AgentSerializer.from_dict(serialized_agent, agent_lifecycle, openai_wrapper) return None def load_all_agents(self, agent_lifecycle, openai_wrapper): """<fim_suffix><fim_middle> Load all agents from the database. """
Load all agents from the database. """
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/integrations/sqlite_agent_persistence.py<fim_prefix>import sqlite3 import json from integrations.agent_persistence import AbstractAgentPersistence class SQLiteAgentPersistence(AbstractAgentPersistence): def __init__(self, filename="agents.db"): self.filename = filename self._initialize_database() def _initialize_database(self): """ Initialize the SQLite database with the required schema. """ with sqlite3.connect(self.filename) as conn: conn.execute(""" CREATE TABLE IF NOT EXISTS agents ( id TEXT PRIMARY KEY, purpose TEXT, data TEXT ) """) def remove_agent(self, purpose): """ Remove an agent from the SQLite database. """ with sqlite3.connect(self.filename) as conn: conn.execute("DELETE FROM agents WHERE id = ?", (purpose,)) def save_agent(self, agent_dict): """<fim_suffix><fim_middle> Save the serialized agent to an SQLite database. """
Save the serialized agent to an SQLite database. """
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/agent_lifecycle.py<fim_prefix>import logging from typing import List from agents.microagent import MicroAgent from integrations.openaiwrapper import OpenAIAPIWrapper from agents.agent_similarity import AgentSimilarity from agents.agent_persistence_manager import AgentPersistenceManager from numpy import ndarray from prompt_management.prompts import ( PRIME_PROMPT, PRIME_NAME, PROMPT_ENGINEERING_SYSTEM_PROMPT, PROMPT_ENGINEERING_TEMPLATE, EXAMPLES ) logger = logging.getLogger() DEFAULT_MAX_AGENTS = 2000 PRIME_AGENT_WEIGHT = 25 class AgentLifecycle: def __init__(self, openai_wrapper: OpenAIAPIWrapper, agent_persistence_manager: AgentPersistenceManager, max_agents: int = DEFAULT_MAX_AGENTS): self.agents: List[MicroAgent] = [] self.openai_wrapper = openai_wrapper self.agent_persistence = agent_persistence_manager self.max_agents = max_agents def stop_all_agents(self) -> None: """Stops all agents.""" for agent in self.agents: agent.stop() def reset_all_agents(self) -> None: """Resets all agents.""" for agent in self.agents: agent.reset() def cleanup_agents(self): """Remove<fim_suffix><fim_middle> all agents with status stopped = True in an efficient manner."""
all agents with status stopped = True in an efficient manner."""
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/agent_lifecycle.py<fim_prefix>import logging from typing import List from agents.microagent import MicroAgent from integrations.openaiwrapper import OpenAIAPIWrapper from agents.agent_similarity import AgentSimilarity from agents.agent_persistence_manager import AgentPersistenceManager from numpy import ndarray from prompt_management.prompts import ( PRIME_PROMPT, PRIME_NAME, PROMPT_ENGINEERING_SYSTEM_PROMPT, PROMPT_ENGINEERING_TEMPLATE, EXAMPLES ) logger = logging.getLogger() DEFAULT_MAX_AGENTS = 2000 PRIME_AGENT_WEIGHT = 25 class AgentLifecycle: def __init__(self, openai_wrapper: OpenAIAPIWrapper, agent_persistence_manager: AgentPersistenceManager, max_agents: int = DEFAULT_MAX_AGENTS): self.agents: List[MicroAgent] = [] self.openai_wrapper = openai_wrapper self.agent_persistence = agent_persistence_manager self.max_agents = max_agents def stop_all_agents(self) -> None: """Stops all agents.""" for agent in self.agents: agent.stop() def reset_all_agents(self) -> None: """Resets all agents.""" for agent in self.agents: agent.reset() def cleanup_agents(self): """Remove all agents with status stopped = True in an efficient manner.""" self.agents = [agent for agent in self.agents if not agent.stopped] def create_prime_agent(self) -> None: """Creates the prime agent and adds it to the agent list.""" prime_agent = MicroAgent( PRIME_PROMPT, PRIME_NAME, 0, self, self.openai_wrapper, PRIME_AGENT_WEIGHT, True, True ) self.agents.append(prime_agent) def add_agent(self, agent: MicroAgent) -> None: """Adds an agent to the list of agents.""" self.agents.append(agent) def get_available_agents_for_agent(self, agent) -> List[MicroAgent]: """Returns the list of available agents for the given purpose.""" agent_id = agent.id available_agents = [agent for agent in self.agents if agent.purpose != "Bootstrap Agent" and agent.working_agent] for agent in available_agents: if agent.parent_id != agent_id: available_agents.remove(agent) return available_agents def get_or_create_agent(self, purpose: str, depth: int, sample_input: str, force_new: bool = False, parent_agent=None) -> MicroAgent: """ Retrieves or creates an agent based on the given purpose. Optionally creates a new agent regardless of similarity if force_new is True. """ if not force_new: agent_similarity = AgentSimilarity(self.openai_wrapper, self.agents) purpose_embedding = agent_similarity.get_embedding(purpose) closest_agent, highest_similarity = agent_similarity.find_closest_agent(purpose_embedding) similarity_threshold = agent_similarity.calculate_similarity_threshold() if highest_similarity >= similarity_threshold: closest_agent.usage_count += 1 return closest_agent return self._create_and_add_agent(purpose, depth, sample_input, parent_agent=parent_agent) def _create_and_add_agent(self, purpose: str, depth: int, sample_input: str, parent_agent=None) -> MicroAgent: """Helper method to create and add a new agent.""" if len(self.agents) >= self.max_agents: self._remove_least_used_agent() new_agent = MicroAgent(self._generate_llm_prompt(purpose, sample_input), purpose, depth, self, self.openai_wrapper, parent=parent_agent) new_agent.usage_count = 1 self.agents.append(new_agent) return new_agent def _remove_least_used_agent(self): """Removes the least used agent.""" least_used_agent = min(self.agents, key=lambda agent: agent.usage_count) self.agents.remove(least_used_agent) def save_agent(self, agent: MicroAgent) -> None: """Saves the given agent with error handling.""" try: self.agent_persistence.save_agent(agent) except Exception as e: logger.exception(f"Error in saving agent: {e}") raise def remove_agent(self, agent: MicroAgent) -> None: """Removes the given agent with error handling.""" try: self.agent_persistence.remove_agent(agent) except Exception as e: logger.exception(f"Error in saving agent: {e}") raise def _generate_llm_prompt(self, goal: str, sample_input: str) -> str: """<fim_suffix><fim_middle> Generates a prompt for the LLM based on the given goal and sample input. """
Generates a prompt for the LLM based on the given goal and sample input. """
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/integrations/memoize.py<fim_prefix>import sqlite3 import hashlib import json import functools ## Originally from https://www.kevinkatz.io/posts/memoize-to-sqlite def memoize_to_sqlite(func_name: str, filename: str = "cache.db"): """<fim_suffix><fim_middle> Memoization decorator that caches the output of a method in a SQLite database. """
Memoization decorator that caches the output of a method in a SQLite database. """
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/agent_lifecycle.py<fim_prefix>import logging from typing import List from agents.microagent import MicroAgent from integrations.openaiwrapper import OpenAIAPIWrapper from agents.agent_similarity import AgentSimilarity from agents.agent_persistence_manager import AgentPersistenceManager from numpy import ndarray from prompt_management.prompts import ( PRIME_PROMPT, PRIME_NAME, PROMPT_ENGINEERING_SYSTEM_PROMPT, PROMPT_ENGINEERING_TEMPLATE, EXAMPLES ) logger = logging.getLogger() DEFAULT_MAX_AGENTS = 2000 PRIME_AGENT_WEIGHT = 25 class AgentLifecycle: def __init__(self, openai_wrapper: OpenAIAPIWrapper, agent_persistence_manager: AgentPersistenceManager, max_agents: int = DEFAULT_MAX_AGENTS): self.agents: List[MicroAgent] = [] self.openai_wrapper = openai_wrapper self.agent_persistence = agent_persistence_manager self.max_agents = max_agents def stop_all_agents(self) -> None: """Stops all agents.""" for agent in self.agents: agent.stop() def reset_all_agents(self) -> None: """Resets all agents.""" for agent in self.agents: agent.reset() def cleanup_agents(self): """Remove all agents with status stopped = True in an efficient manner.""" self.agents = [agent for agent in self.agents if not agent.stopped] def create_prime_agent(self) -> None: """Creates<fim_suffix><fim_middle> the prime agent and adds it to the agent list."""
the prime agent and adds it to the agent list."""
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/microagent_manager.py<fim_prefix>import logging from typing import List, Optional, Any from agents.agent_lifecycle import AgentLifecycle from agents.agent_similarity import AgentSimilarity from agents.agent_persistence_manager import AgentPersistenceManager from integrations.openaiwrapper import OpenAIAPIWrapper logger= logging.getLogger() class MicroAgentManager: """ Manages the creation and retrieval of micro agents. """ def __init__(self, openai_wrapper: OpenAIAPIWrapper, max_agents: int = 20, db_filename : str = "agents.db"): self.max_agents = max_agents self.openai_wrapper = openai_wrapper self.agent_persistence = AgentPersistenceManager(db_filename) self.agent_lifecycle = AgentLifecycle(self.openai_wrapper, self.agent_persistence, max_agents) self.load_agents() def stop_all_agents(self) -> None: """Stops all agents.""" self.agent_lifecycle.stop_all_agents() def cleanup_agents(self): """Remove all agents with status stopped = True""" self.agent_lifecycle.cleanup_agents() def load_agents(self): """Loads agents from the database.""" loaded_agents = self.agent_persistence.load_all_agents(self.agent_lifecycle, self.openai_wrapper) self.agent_lifecycle.agents.extend(loaded_agents) logger.info(f"Loaded {len(loaded_agents)} agents from the database.") def get_agents(self) -> List[Any]: """Returns<fim_suffix><fim_middle> the list of agents."""
the list of agents."""
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/agent_similarity.py<fim_prefix>import logging import numpy as np from typing import List, Tuple, Optional from sklearn.metrics.pairwise import cosine_similarity from integrations.openaiwrapper import OpenAIAPIWrapper logger = logging.getLogger() class Agent: def __init__(self, purpose: str): self.purpose = purpose self.purpose_embedding=None class AgentSimilarity: def __init__(self, openai_wrapper: OpenAIAPIWrapper, agents: List[Agent]): """ Initializes the AgentSimilarity object. :param openai_wrapper: Instance of OpenAIAPIWrapper to interact with OpenAI API. :param agents: List of Agent objects. """ self.openai_wrapper = openai_wrapper self.agents = agents def get_embedding(self, text: str) -> np.ndarray: """ Retrieves the embedding for a given text. :param text: Text to get embedding for. :return: Embedding as a numpy array. """ try: response = self.openai_wrapper.get_embedding(text) if 'data' in response and len(response['data']) > 0 and 'embedding' in response['data'][0]: return np.array(response['data'][0]['embedding']) else: logger.exception("Invalid response format") raise ValueError("Invalid response format") except Exception as e: logger.exception(f"Error retrieving embedding: {e}") raise ValueError(f"Error retrieving embedding: {e}") def calculate_similarity_threshold(self) -> float: """ Calculates the 98th percentile of the similarity threshold across all agents. :return: 98th percentile of similarity threshold. """ try: embeddings=[] for agent in self.agents: if agent.purpose_embedding is None: agent.purpose_embedding = self.get_embedding(agent.purpose) embeddings.append(agent.purpose_embedding) if len(embeddings) < 250: return 0.999 similarities = [cosine_similarity([e1], [e2])[0][0] for i, e1 in enumerate(embeddings) for e2 in embeddings[i+1:]] return np.percentile(similarities, 98) if similarities else 0.999 except Exception as e: logger.exception(f"Error calculating similarity threshold: {e}") raise ValueError(f"Error calculating similarity threshold: {e}") def find_closest_agent(self, purpose_embedding: np.ndarray) -> Tuple[Optional[Agent], float]: """<fim_suffix><fim_middle> Finds the closest agent based on the given purpose embedding. :param purpose_embedding: The embedding of the purpose to find the closest agent for. :return: Tuple of the closest agent and the highest similarity score. """
Finds the closest agent based on the given purpose embedding. :param purpose_embedding: The embedding of the purpose to find the closest agent for. :return: Tuple of the closest agent and the highest similarity score. """
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/microagent_manager.py<fim_prefix>import logging from typing import List, Optional, Any from agents.agent_lifecycle import AgentLifecycle from agents.agent_similarity import AgentSimilarity from agents.agent_persistence_manager import AgentPersistenceManager from integrations.openaiwrapper import OpenAIAPIWrapper logger= logging.getLogger() class MicroAgentManager: """ Manages the creation and retrieval of micro agents. """ def __init__(self, openai_wrapper: OpenAIAPIWrapper, max_agents: int = 20, db_filename : str = "agents.db"): self.max_agents = max_agents self.openai_wrapper = openai_wrapper self.agent_persistence = AgentPersistenceManager(db_filename) self.agent_lifecycle = AgentLifecycle(self.openai_wrapper, self.agent_persistence, max_agents) self.load_agents() def stop_all_agents(self) -> None: """Stops all agents.""" self.agent_lifecycle.stop_all_agents() def cleanup_agents(self): """Remove<fim_suffix><fim_middle> all agents with status stopped = True"""
all agents with status stopped = True"""
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/agent_similarity.py<fim_prefix>import logging import numpy as np from typing import List, Tuple, Optional from sklearn.metrics.pairwise import cosine_similarity from integrations.openaiwrapper import OpenAIAPIWrapper logger = logging.getLogger() class Agent: def __init__(self, purpose: str): self.purpose = purpose self.purpose_embedding=None class AgentSimilarity: def __init__(self, openai_wrapper: OpenAIAPIWrapper, agents: List[Agent]): """ Initializes the AgentSimilarity object. :param openai_wrapper: Instance of OpenAIAPIWrapper to interact with OpenAI API. :param agents: List of Agent objects. """ self.openai_wrapper = openai_wrapper self.agents = agents def get_embedding(self, text: str) -> np.ndarray: """<fim_suffix><fim_middle> Retrieves the embedding for a given text. :param text: Text to get embedding for. :return: Embedding as a numpy array. """
Retrieves the embedding for a given text. :param text: Text to get embedding for. :return: Embedding as a numpy array. """
BLOCK_COMMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/microagent.py<fim_prefix>import logging import uuid from integrations.openaiwrapper import OpenAIAPIWrapper from agents.agent_evaluation import AgentEvaluator from agents.agent_response import AgentResponse from agents.agent_similarity import AgentSimilarity from agents.response_extraction import ResponseExtraction from agents.agent_stopped_exception import AgentStoppedException from agents.response_handler import ResponseHandler from runtime.code_execution import CodeExecution from prompt_management.prompt_evolution import PromptEvolution from utils.utility import get_env_variable, time_function, log_exception logger = logging.getLogger() class MicroAgent: """ The MicroAgent class encapsulates the behavior of a small, purpose-driven agent that interacts with the OpenAI API. """ def __init__(self, initial_prompt, purpose, depth, agent_lifecycle, openai_wrapper, max_depth=3, bootstrap_agent=False, is_prime=False, purpose_embedding=None, parent=None, parent_id=None, id=None) : self.dynamic_prompt = initial_prompt self.purpose = purpose self.purpose_embedding = purpose_embedding self.depth = depth self.max_depth = max_depth self.usage_count = 0 self.working_agent = bootstrap_agent self.agent_lifecycle = agent_lifecycle self.openai_wrapper = openai_wrapper self.evolve_count = 0 self.number_of_code_executions = 0 self.current_status = None self.active_agents = {} self.last_input = "" self.last_output = "" self.last_conversation = "" self.stopped = False self.is_prime = is_prime self.stop_execution = False if parent: self.parent_id = parent.id if parent else None else: self.parent_id = None if parent_id: self.parent_id<fim_suffix><fim_middle> = parent_id
= parent_id
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/microagent.py<fim_prefix>import logging import uuid from integrations.openaiwrapper import OpenAIAPIWrapper from agents.agent_evaluation import AgentEvaluator from agents.agent_response import AgentResponse from agents.agent_similarity import AgentSimilarity from agents.response_extraction import ResponseExtraction from agents.agent_stopped_exception import AgentStoppedException from agents.response_handler import ResponseHandler from runtime.code_execution import CodeExecution from prompt_management.prompt_evolution import PromptEvolution from utils.utility import get_env_variable, time_function, log_exception logger = logging.getLogger() class MicroAgent: """ The MicroAgent class encapsulates the behavior of a small, purpose-driven agent that interacts with the OpenAI API. """ def __init__(self, initial_prompt, purpose, depth, agent_lifecycle, openai_wrapper, max_depth=3, bootstrap_agent=False, is_prime=False, purpose_embedding=None, parent=None, parent_id=None, id=None) : self.dynamic_prompt = initial_prompt self.purpose = purpose self.purpose_embedding = purpose_embedding self.depth = depth self.max_depth = max_depth self.usage_count = 0 self.working_agent = bootstrap_agent self.agent_lifecycle = agent_lifecycle self.openai_wrapper = openai_wrapper self.evolve_count = 0 self.number_of_code_executions = 0 self.current_status = None self.active_agents = {} self.last_input = "" self.last_output = "" self.last_conversation = "" self.stopped = False self.is_prime = is_prime self.stop_execution = False if parent: self.parent_id = parent.id if parent else None else: self.parent_id = None if parent_id: self.parent_id = parent_id if is_prime: self.id = "2a5e6fe9-1bb1-426c-9521-145caa2cf66b" else: if id: self.id = id else: self.id<fim_suffix><fim_middle> = str(uuid.uuid4())
= str(uuid.uuid4())
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/integrations/memoize.py<fim_prefix>import sqlite3 import hashlib import json import functools ## Originally from https://www.kevinkatz.io/posts/memoize-to-sqlite def memoize_to_sqlite(func_name: str, filename: str = "cache.db"): """ Memoization decorator that caches the output of a method in a SQLite database. """ def decorator(func): @functools.wraps(func) def wrapped(*args, **kwargs): with SQLiteMemoization(filename) as memoizer: return memoizer.fetch_or_compute(func, func_name, *args, **kwargs) return wrapped return decorator class SQLiteMemoization: def __init__(self, filename): self.filename = filename self.connection = None def __enter__(self): self.connection = sqlite3.connect(self.filename) self._initialize_database() return self def __exit__(self, exc_type, exc_val, exc_tb): self.connection.close() self.connection = None def _initialize_database(self): self.connection.execute( "CREATE TABLE IF NOT EXISTS cache (hash TEXT PRIMARY KEY, result TEXT)" ) self.connection.execute( "CREATE INDEX IF NOT EXISTS cache_ndx ON cache(hash)" ) def fetch_or_compute(self, func, func_name, *args, **kwargs): arg_hash = self._compute_hash(func_name, *args, **kwargs) result<fim_suffix><fim_middle> = self._fetch_from_cache(arg_hash)
= self._fetch_from_cache(arg_hash)
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/microagent.py<fim_prefix>import logging import uuid from integrations.openaiwrapper import OpenAIAPIWrapper from agents.agent_evaluation import AgentEvaluator from agents.agent_response import AgentResponse from agents.agent_similarity import AgentSimilarity from agents.response_extraction import ResponseExtraction from agents.agent_stopped_exception import AgentStoppedException from agents.response_handler import ResponseHandler from runtime.code_execution import CodeExecution from prompt_management.prompt_evolution import PromptEvolution from utils.utility import get_env_variable, time_function, log_exception logger = logging.getLogger() class MicroAgent: """ The MicroAgent class encapsulates the behavior of a small, purpose-driven agent that interacts with the OpenAI API. """ def __init__(self, initial_prompt, purpose, depth, agent_lifecycle, openai_wrapper, max_depth=3, bootstrap_agent=False, is_prime=False, purpose_embedding=None, parent=None, parent_id=None, id=None) : self.dynamic_prompt<fim_suffix><fim_middle> = initial_prompt
= initial_prompt
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/integrations/sqlite_agent_persistence.py<fim_prefix>import sqlite3 import json from integrations.agent_persistence import AbstractAgentPersistence class SQLiteAgentPersistence(AbstractAgentPersistence): def __init__(self, filename="agents.db"): self.filename = filename self._initialize_database() def _initialize_database(self): """ Initialize the SQLite database with the required schema. """ with sqlite3.connect(self.filename) as conn: conn.execute(""" CREATE TABLE IF NOT EXISTS agents ( id TEXT PRIMARY KEY, purpose TEXT, data TEXT ) """) def remove_agent(self, purpose): """ Remove an agent from the SQLite database. """ with sqlite3.connect(self.filename) as conn: conn.execute("DELETE FROM agents WHERE id = ?", (purpose,)) def save_agent(self, agent_dict): """ Save the serialized agent to an SQLite database. """ with sqlite3.connect(self.filename) as conn: conn.execute( # add id field "REPLACE INTO agents (id, purpose, data) VALUES (?, ?, ?)", (agent_dict['id'], agent_dict['purpose'], json.dumps(agent_dict)) ) def fetch_agent(self, purpose): """ Fetch a serialized agent based on its purpose from the SQLite database. """ with sqlite3.connect(self.filename) as conn: cursor = conn.cursor() cursor.execute("SELECT data FROM agents WHERE purpose = ?", (purpose,)) row = cursor.fetchone() return json.loads(row[0]) if row else None def load_all_purposes(self): """ Load all agent purposes from the SQLite database. """ with sqlite3.connect(self.filename) as conn: cursor<fim_suffix><fim_middle> = conn.cursor()
= conn.cursor()
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/agent_similarity.py<fim_prefix>import logging import numpy as np from typing import List, Tuple, Optional from sklearn.metrics.pairwise import cosine_similarity from integrations.openaiwrapper import OpenAIAPIWrapper logger = logging.getLogger() class Agent: def __init__(self, purpose: str): self.purpose = purpose self.purpose_embedding=None class AgentSimilarity: def __init__(self, openai_wrapper: OpenAIAPIWrapper, agents: List[Agent]): """ Initializes the AgentSimilarity object. :param openai_wrapper: Instance of OpenAIAPIWrapper to interact with OpenAI API. :param agents: List of Agent objects. """ self.openai_wrapper = openai_wrapper self.agents = agents def get_embedding(self, text: str) -> np.ndarray: """ Retrieves the embedding for a given text. :param text: Text to get embedding for. :return: Embedding as a numpy array. """ try: response = self.openai_wrapper.get_embedding(text) if 'data' in response and len(response['data']) > 0 and 'embedding' in response['data'][0]: return np.array(response['data'][0]['embedding']) else: logger.exception("Invalid response format") raise ValueError("Invalid response format") except Exception as e: logger.exception(f"Error retrieving embedding: {e}") raise ValueError(f"Error retrieving embedding: {e}") def calculate_similarity_threshold(self) -> float: """ Calculates the 98th percentile of the similarity threshold across all agents. :return: 98th percentile of similarity threshold. """ try: embeddings=[] for agent in self.agents: if agent.purpose_embedding is None: agent.purpose_embedding = self.get_embedding(agent.purpose) embeddings.append(agent.purpose_embedding) if len(embeddings) < 250: return 0.999 similarities = [cosine_similarity([e1], [e2])[0][0] for i, e1 in enumerate(embeddings) for e2 in embeddings[i+1:]] return np.percentile(similarities, 98) if similarities else 0.999 except Exception as e: logger.exception(f"Error calculating similarity threshold: {e}") raise ValueError(f"Error calculating similarity threshold: {e}") def find_closest_agent(self, purpose_embedding: np.ndarray) -> Tuple[Optional[Agent], float]: """ Finds the closest agent based on the given purpose embedding. :param purpose_embedding: The embedding of the purpose to find the closest agent for. :return: Tuple of the closest agent and the highest similarity score. """ closest_agent: Optional[Agent] = None highest_similarity: float = -np.inf try: for agent in self.agents: if agent.purpose_embedding is None: agent.purpose_embedding = self.get_embedding(agent.purpose) similarity<fim_suffix><fim_middle> = cosine_similarity([agent.purpose_embedding], [purpose_embedding])[0][0]
= cosine_similarity([agent.purpose_embedding], [purpose_embedding])[0][0]
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/integrations/memoize.py<fim_prefix>import sqlite3 import hashlib import json import functools ## Originally from https://www.kevinkatz.io/posts/memoize-to-sqlite def memoize_to_sqlite(func_name: str, filename: str = "cache.db"): """ Memoization decorator that caches the output of a method in a SQLite database. """ def decorator(func): @functools.wraps(func) def wrapped(*args, **kwargs): with SQLiteMemoization(filename) as memoizer: return memoizer.fetch_or_compute(func, func_name, *args, **kwargs) return wrapped return decorator class SQLiteMemoization: def __init__(self, filename): self.filename = filename self.connection = None def __enter__(self): self.connection = sqlite3.connect(self.filename) self._initialize_database() return self def __exit__(self, exc_type, exc_val, exc_tb): self.connection.close() self.connection = None def _initialize_database(self): self.connection.execute( "CREATE TABLE IF NOT EXISTS cache (hash TEXT PRIMARY KEY, result TEXT)" ) self.connection.execute( "CREATE INDEX IF NOT EXISTS cache_ndx ON cache(hash)" ) def fetch_or_compute(self, func, func_name, *args, **kwargs): arg_hash = self._compute_hash(func_name, *args, **kwargs) result = self._fetch_from_cache(arg_hash) if result is not None: return result return self._compute_and_cache_result(func, arg_hash, *args, **kwargs) def _compute_hash(self, func_name, *args, **kwargs): data = f"{func_name}:{repr(args)}:{repr(kwargs)}".encode("utf-8") return hashlib.sha256(data).hexdigest() def _fetch_from_cache(self, arg_hash): cursor = self.connection.cursor() cursor.execute("SELECT result FROM cache WHERE hash = ?", (arg_hash,)) row = cursor.fetchone() return json.loads(row[0]) if row else None def _compute_and_cache_result(self, func, arg_hash, *args, **kwargs): result = func(*args, **kwargs) self._cache_result(arg_hash, result) return result def _cache_result(self, arg_hash, result): cursor<fim_suffix><fim_middle> = self.connection.cursor()
= self.connection.cursor()
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/microagent.py<fim_prefix>import logging import uuid from integrations.openaiwrapper import OpenAIAPIWrapper from agents.agent_evaluation import AgentEvaluator from agents.agent_response import AgentResponse from agents.agent_similarity import AgentSimilarity from agents.response_extraction import ResponseExtraction from agents.agent_stopped_exception import AgentStoppedException from agents.response_handler import ResponseHandler from runtime.code_execution import CodeExecution from prompt_management.prompt_evolution import PromptEvolution from utils.utility import get_env_variable, time_function, log_exception logger = logging.getLogger() class MicroAgent: """ The MicroAgent class encapsulates the behavior of a small, purpose-driven agent that interacts with the OpenAI API. """ def __init__(self, initial_prompt, purpose, depth, agent_lifecycle, openai_wrapper, max_depth=3, bootstrap_agent=False, is_prime=False, purpose_embedding=None, parent=None, parent_id=None, id=None) : self.dynamic_prompt = initial_prompt self.purpose = purpose self.purpose_embedding = purpose_embedding self.depth = depth self.max_depth = max_depth self.usage_count = 0 self.working_agent = bootstrap_agent self.agent_lifecycle = agent_lifecycle self.openai_wrapper = openai_wrapper self.evolve_count = 0 self.number_of_code_executions = 0 self.current_status = None self.active_agents = {} self.last_input = "" self.last_output = "" self.last_conversation = "" self.stopped = False self.is_prime = is_prime self.stop_execution = False if parent: self.parent_id = parent.id if parent else None else: self.parent_id<fim_suffix><fim_middle> = None
= None
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/integrations/memoize.py<fim_prefix>import sqlite3 import hashlib import json import functools ## Originally from https://www.kevinkatz.io/posts/memoize-to-sqlite def memoize_to_sqlite(func_name: str, filename: str = "cache.db"): """ Memoization decorator that caches the output of a method in a SQLite database. """ def decorator(func): @functools.wraps(func) def wrapped(*args, **kwargs): with SQLiteMemoization(filename) as memoizer: return memoizer.fetch_or_compute(func, func_name, *args, **kwargs) return wrapped return decorator class SQLiteMemoization: def __init__(self, filename): self.filename = filename self.connection = None def __enter__(self): self.connection<fim_suffix><fim_middle> = sqlite3.connect(self.filename)
= sqlite3.connect(self.filename)
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/microagent.py<fim_prefix>import logging import uuid from integrations.openaiwrapper import OpenAIAPIWrapper from agents.agent_evaluation import AgentEvaluator from agents.agent_response import AgentResponse from agents.agent_similarity import AgentSimilarity from agents.response_extraction import ResponseExtraction from agents.agent_stopped_exception import AgentStoppedException from agents.response_handler import ResponseHandler from runtime.code_execution import CodeExecution from prompt_management.prompt_evolution import PromptEvolution from utils.utility import get_env_variable, time_function, log_exception logger = logging.getLogger() class MicroAgent: """ The MicroAgent class encapsulates the behavior of a small, purpose-driven agent that interacts with the OpenAI API. """ def __init__(self, initial_prompt, purpose, depth, agent_lifecycle, openai_wrapper, max_depth=3, bootstrap_agent=False, is_prime=False, purpose_embedding=None, parent=None, parent_id=None, id=None) : self.dynamic_prompt = initial_prompt self.purpose = purpose self.purpose_embedding = purpose_embedding self.depth<fim_suffix><fim_middle> = depth
= depth
STATEMENT
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/agent_lifecycle.py<fim_prefix>import logging from typing import List from agents.microagent import MicroAgent from integrations.openaiwrapper import OpenAIAPIWrapper from agents.agent_similarity import AgentSimilarity from agents.agent_persistence_manager import AgentPersistenceManager from numpy import ndarray from prompt_management.prompts import ( PRIME_PROMPT, PRIME_NAME, PROMPT_ENGINEERING_SYSTEM_PROMPT, PROMPT_ENGINEERING_TEMPLATE, EXAMPLES ) logger = logging.getLogger() DEFAULT_MAX_AGENTS = 2000 PRIME_AGENT_WEIGHT = 25 class AgentLifecycle: def __init__(self, openai_wrapper: OpenAIAPIWrapper, agent_persistence_manager: AgentPersistenceManager, max_agents: int = DEFAULT_MAX_AGENTS): self.agents: List[MicroAgent] = [] self.openai_wrapper = openai_wrapper self.agent_persistence = agent_persistence_manager self.max_agents = max_agents def stop_all_agents(self) -> None: """Stops all agents.""" for agent in self.agents: agent.stop() def reset_all_agents(self) -> None: """Resets all agents.""" for agent in self.agents: agent.reset() def cleanup_agents(self): """Remove all agents with status stopped = True in an efficient manner.""" self.agents = [agent for agent in self.agents if not agent.stopped] def create_prime_agent(self) -> None: """Creates the prime agent and adds it to the agent list.""" prime_agent = MicroAgent( PRIME_PROMPT, PRIME_NAME, 0, self, self.openai_wrapper, PRIME_AGENT_WEIGHT, True, True ) self.agents.append(prime_agent) def add_agent(self, agent: MicroAgent) -> None: """Adds an agent to the list of agents.""" self.agents.append(agent) def get_available_agents_for_agent(self, agent) -> List[MicroAgent]: """Returns the list of available agents for the given purpose.""" agent_id = agent.id available_agents = [agent for agent in self.agents if agent.purpose != "Bootstrap Agent" and agent.working_agent] for agent in available_agents: if agent.parent_id != agent_id: available_agents.remove(agent) return available_agents def get_or_create_agent(self, purpose: str, depth: int, sample_input: str, force_new: bool = False, parent_agent=None) -> MicroAgent: """ Retrieves or creates an agent based on the given purpose. Optionally creates a new agent regardless of similarity if force_new is True. """ if not force_new: agent_similarity = AgentSimilarity(self.openai_wrapper, self.agents) purpose_embedding = agent_similarity.get_embedding(purpose) closest_agent, highest_similarity = agent_similarity.find_closest_agent(purpose_embedding) similarity_threshold = agent_similarity.calculate_similarity_threshold() if highest_similarity >= similarity_threshold: closest_agent.usage_count += 1 return closest_agent return self._create_and_add_agent(purpose, depth, sample_input, parent_agent=parent_agent) def _create_and_add_agent(self, purpose: str, depth: int, sample_input: str, parent_agent=None) -> MicroAgent: """Helper method to create and add a new agent.""" if len(self.agents) >= self.max_agents: self._remove_least_used_agent() new_agent = MicroAgent(self._generate_llm_prompt(purpose, sample_input), purpose, depth, self, self.openai_wrapper, parent=parent_agent) new_agent.usage_count = 1 self.agents.append(new_agent) return new_agent def _remove_least_used_agent(self): """Removes the least used agent.""" least_used_agent = min(self.agents, key=lambda agent: agent.usage_count) self.agents.remove(least_used_agent) def save_agent(self, agent: MicroAgent) -> None: """Saves the given agent with error handling.""" try: self.agent_persistence.save_agent(agent) except Exception as e: logger.exception(f"Error in saving agent: {e}") raise def remove_agent(self, agent: MicroAgent) -> None: """Removes the given agent with error handling.""" try: self.agent_persistence.remove_agent(agent) except Exception as e: logger.exception(f"Error in saving agent: {e}") raise def _generate_llm_prompt(self, goal: str, sample_input: str) -> str: """ Generates a prompt for the LLM based on the given goal and sample input. """ messages = [ {"role": "system", "content": PROMPT_ENGINEERING_SYSTEM_PROMPT}, {"role": "user", "content": PROMPT_ENGINEERING_TEMPLATE.format(goal=goal, sample_input=sample_input, examples=EXAMPLES)} ] try:<fim_suffix><fim_middle> return self.openai_wrapper.chat_completion(messages=messages)
return self.openai_wrapper.chat_completion(messages=messages)
TRY
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/agent_similarity.py<fim_prefix>import logging import numpy as np from typing import List, Tuple, Optional from sklearn.metrics.pairwise import cosine_similarity from integrations.openaiwrapper import OpenAIAPIWrapper logger = logging.getLogger() class Agent: def __init__(self, purpose: str): self.purpose = purpose self.purpose_embedding=None class AgentSimilarity: def __init__(self, openai_wrapper: OpenAIAPIWrapper, agents: List[Agent]): """ Initializes the AgentSimilarity object. :param openai_wrapper: Instance of OpenAIAPIWrapper to interact with OpenAI API. :param agents: List of Agent objects. """ self.openai_wrapper = openai_wrapper self.agents = agents def get_embedding(self, text: str) -> np.ndarray: """ Retrieves the embedding for a given text. :param text: Text to get embedding for. :return: Embedding as a numpy array. """ try: response = self.openai_wrapper.get_embedding(text) if 'data' in response and len(response['data']) > 0 and 'embedding' in response['data'][0]: return np.array(response['data'][0]['embedding']) else: logger.exception("Invalid response format") raise ValueError("Invalid response format") except Exception as e: logger.exception(f"Error retrieving embedding: {e}") raise ValueError(f"Error retrieving embedding: {e}") def calculate_similarity_threshold(self) -> float: """ Calculates the 98th percentile of the similarity threshold across all agents. :return: 98th percentile of similarity threshold. """ try: embeddings=[] for agent in self.agents: if agent.purpose_embedding is None: agent.purpose_embedding = self.get_embedding(agent.purpose) embeddings.append(agent.purpose_embedding) if len(embeddings) < 250: return 0.999 similarities = [cosine_similarity([e1], [e2])[0][0] for i, e1 in enumerate(embeddings) for e2 in embeddings[i+1:]] return np.percentile(similarities, 98) if similarities else 0.999 except Exception as e: logger.exception(f"Error calculating similarity threshold: {e}") raise ValueError(f"Error calculating similarity threshold: {e}") def find_closest_agent(self, purpose_embedding: np.ndarray) -> Tuple[Optional[Agent], float]: """ Finds the closest agent based on the given purpose embedding. :param purpose_embedding: The embedding of the purpose to find the closest agent for. :return: Tuple of the closest agent and the highest similarity score. """ closest_agent: Optional[Agent] = None highest_similarity: float = -np.inf try:<fim_suffix><fim_middle> for agent in self.agents: if agent.purpose_embedding is None: agent.purpose_embedding = self.get_embedding(agent.purpose) similarity = cosine_similarity([agent.purpose_embedding], [purpose_embedding])[0][0] if similarity > highest_similarity: highest_similarity = similarity closest_agent = agent return closest_agent, highest_similarity
for agent in self.agents: if agent.purpose_embedding is None: agent.purpose_embedding = self.get_embedding(agent.purpose) similarity = cosine_similarity([agent.purpose_embedding], [purpose_embedding])[0][0] if similarity > highest_similarity: highest_similarity = similarity closest_agent = agent return closest_agent, highest_similarity
TRY
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/agent_similarity.py<fim_prefix>import logging import numpy as np from typing import List, Tuple, Optional from sklearn.metrics.pairwise import cosine_similarity from integrations.openaiwrapper import OpenAIAPIWrapper logger = logging.getLogger() class Agent: def __init__(self, purpose: str): self.purpose = purpose self.purpose_embedding=None class AgentSimilarity: def __init__(self, openai_wrapper: OpenAIAPIWrapper, agents: List[Agent]): """ Initializes the AgentSimilarity object. :param openai_wrapper: Instance of OpenAIAPIWrapper to interact with OpenAI API. :param agents: List of Agent objects. """ self.openai_wrapper = openai_wrapper self.agents = agents def get_embedding(self, text: str) -> np.ndarray: """ Retrieves the embedding for a given text. :param text: Text to get embedding for. :return: Embedding as a numpy array. """ try:<fim_suffix><fim_middle> response = self.openai_wrapper.get_embedding(text) if 'data' in response and len(response['data']) > 0 and 'embedding' in response['data'][0]: return np.array(response['data'][0]['embedding']) else: logger.exception("Invalid response format") raise ValueError("Invalid response format")
response = self.openai_wrapper.get_embedding(text) if 'data' in response and len(response['data']) > 0 and 'embedding' in response['data'][0]: return np.array(response['data'][0]['embedding']) else: logger.exception("Invalid response format") raise ValueError("Invalid response format")
TRY
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/agent_lifecycle.py<fim_prefix>import logging from typing import List from agents.microagent import MicroAgent from integrations.openaiwrapper import OpenAIAPIWrapper from agents.agent_similarity import AgentSimilarity from agents.agent_persistence_manager import AgentPersistenceManager from numpy import ndarray from prompt_management.prompts import ( PRIME_PROMPT, PRIME_NAME, PROMPT_ENGINEERING_SYSTEM_PROMPT, PROMPT_ENGINEERING_TEMPLATE, EXAMPLES ) logger = logging.getLogger() DEFAULT_MAX_AGENTS = 2000 PRIME_AGENT_WEIGHT = 25 class AgentLifecycle: def __init__(self, openai_wrapper: OpenAIAPIWrapper, agent_persistence_manager: AgentPersistenceManager, max_agents: int = DEFAULT_MAX_AGENTS): self.agents: List[MicroAgent] = [] self.openai_wrapper = openai_wrapper self.agent_persistence = agent_persistence_manager self.max_agents = max_agents def stop_all_agents(self) -> None: """Stops all agents.""" for agent in self.agents: agent.stop() def reset_all_agents(self) -> None: """Resets all agents.""" for agent in self.agents: agent.reset() def cleanup_agents(self): """Remove all agents with status stopped = True in an efficient manner.""" self.agents = [agent for agent in self.agents if not agent.stopped] def create_prime_agent(self) -> None: """Creates the prime agent and adds it to the agent list.""" prime_agent = MicroAgent( PRIME_PROMPT, PRIME_NAME, 0, self, self.openai_wrapper, PRIME_AGENT_WEIGHT, True, True ) self.agents.append(prime_agent) def add_agent(self, agent: MicroAgent) -> None: """Adds an agent to the list of agents.""" self.agents.append(agent) def get_available_agents_for_agent(self, agent) -> List[MicroAgent]: """Returns the list of available agents for the given purpose.""" agent_id = agent.id available_agents = [agent for agent in self.agents if agent.purpose != "Bootstrap Agent" and agent.working_agent] for agent in available_agents: if agent.parent_id != agent_id: available_agents.remove(agent) return available_agents def get_or_create_agent(self, purpose: str, depth: int, sample_input: str, force_new: bool = False, parent_agent=None) -> MicroAgent: """ Retrieves or creates an agent based on the given purpose. Optionally creates a new agent regardless of similarity if force_new is True. """ if not force_new: agent_similarity = AgentSimilarity(self.openai_wrapper, self.agents) purpose_embedding = agent_similarity.get_embedding(purpose) closest_agent, highest_similarity = agent_similarity.find_closest_agent(purpose_embedding) similarity_threshold = agent_similarity.calculate_similarity_threshold() if highest_similarity >= similarity_threshold: closest_agent.usage_count += 1 return closest_agent return self._create_and_add_agent(purpose, depth, sample_input, parent_agent=parent_agent) def _create_and_add_agent(self, purpose: str, depth: int, sample_input: str, parent_agent=None) -> MicroAgent: """Helper method to create and add a new agent.""" if len(self.agents) >= self.max_agents: self._remove_least_used_agent() new_agent = MicroAgent(self._generate_llm_prompt(purpose, sample_input), purpose, depth, self, self.openai_wrapper, parent=parent_agent) new_agent.usage_count = 1 self.agents.append(new_agent) return new_agent def _remove_least_used_agent(self): """Removes the least used agent.""" least_used_agent = min(self.agents, key=lambda agent: agent.usage_count) self.agents.remove(least_used_agent) def save_agent(self, agent: MicroAgent) -> None: """Saves the given agent with error handling.""" try:<fim_suffix><fim_middle> self.agent_persistence.save_agent(agent)
self.agent_persistence.save_agent(agent)
TRY
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/agent_similarity.py<fim_prefix>import logging import numpy as np from typing import List, Tuple, Optional from sklearn.metrics.pairwise import cosine_similarity from integrations.openaiwrapper import OpenAIAPIWrapper logger = logging.getLogger() class Agent: def __init__(self, purpose: str): self.purpose = purpose self.purpose_embedding=None class AgentSimilarity: def __init__(self, openai_wrapper: OpenAIAPIWrapper, agents: List[Agent]): """ Initializes the AgentSimilarity object. :param openai_wrapper: Instance of OpenAIAPIWrapper to interact with OpenAI API. :param agents: List of Agent objects. """ self.openai_wrapper = openai_wrapper self.agents = agents def get_embedding(self, text: str) -> np.ndarray: """ Retrieves the embedding for a given text. :param text: Text to get embedding for. :return: Embedding as a numpy array. """ try: response = self.openai_wrapper.get_embedding(text) if 'data' in response and len(response['data']) > 0 and 'embedding' in response['data'][0]: return np.array(response['data'][0]['embedding']) else: logger.exception("Invalid response format") raise ValueError("Invalid response format") except Exception as e: logger.exception(f"Error retrieving embedding: {e}") raise ValueError(f"Error retrieving embedding: {e}") def calculate_similarity_threshold(self) -> float: """ Calculates the 98th percentile of the similarity threshold across all agents. :return: 98th percentile of similarity threshold. """ try: embeddings=[] for agent in self.agents: if agent.purpose_embedding is None: agent.purpose_embedding = self.get_embedding(agent.purpose) embeddings.append(agent.purpose_embedding) if len(embeddings) < 250: return 0.999 similarities = [cosine_similarity([e1], [e2])[0][0] for i, e1 in enumerate(embeddings) for e2 in embeddings[i+1:]] return np.percentile(similarities, 98) if similarities else 0.999 except Exception as e: logger.exception(f"Error calculating similarity threshold: {e}") raise ValueError(f"Error calculating similarity threshold: {e}") def find_closest_agent(self, purpose_embedding: np.ndarray) -> Tuple[Optional[Agent], float]: """ Finds the closest agent based on the given purpose embedding. :param purpose_embedding: The embedding of the purpose to find the closest agent for. :return: Tuple of the closest agent and the highest similarity score. """ closest_agent: Optional[Agent] = None highest_similarity: float = -np.inf try: for agent in self.agents: if agent.purpose_embedding is None: agent.purpose_embedding = self.get_embedding(agent.purpose) similarity = cosine_similarity([agent.purpose_embedding], [purpose_embedding])[0][0] if similarity > highest_similarity: highest_similarity = similarity closest_agent = agent return closest_agent, highest_similarity except<fim_suffix><fim_middle> Exception as e: logger.exception(f"Error finding closest agent: {e}") raise ValueError(f"Error finding closest agent: {e}")
Exception as e: logger.exception(f"Error finding closest agent: {e}") raise ValueError(f"Error finding closest agent: {e}")
CATCH
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/agent_lifecycle.py<fim_prefix>import logging from typing import List from agents.microagent import MicroAgent from integrations.openaiwrapper import OpenAIAPIWrapper from agents.agent_similarity import AgentSimilarity from agents.agent_persistence_manager import AgentPersistenceManager from numpy import ndarray from prompt_management.prompts import ( PRIME_PROMPT, PRIME_NAME, PROMPT_ENGINEERING_SYSTEM_PROMPT, PROMPT_ENGINEERING_TEMPLATE, EXAMPLES ) logger = logging.getLogger() DEFAULT_MAX_AGENTS = 2000 PRIME_AGENT_WEIGHT = 25 class AgentLifecycle: def __init__(self, openai_wrapper: OpenAIAPIWrapper, agent_persistence_manager: AgentPersistenceManager, max_agents: int = DEFAULT_MAX_AGENTS): self.agents: List[MicroAgent] = [] self.openai_wrapper = openai_wrapper self.agent_persistence = agent_persistence_manager self.max_agents = max_agents def stop_all_agents(self) -> None: """Stops all agents.""" for agent in self.agents: agent.stop() def reset_all_agents(self) -> None: """Resets all agents.""" for agent in self.agents: agent.reset() def cleanup_agents(self): """Remove all agents with status stopped = True in an efficient manner.""" self.agents = [agent for agent in self.agents if not agent.stopped] def create_prime_agent(self) -> None: """Creates the prime agent and adds it to the agent list.""" prime_agent = MicroAgent( PRIME_PROMPT, PRIME_NAME, 0, self, self.openai_wrapper, PRIME_AGENT_WEIGHT, True, True ) self.agents.append(prime_agent) def add_agent(self, agent: MicroAgent) -> None: """Adds an agent to the list of agents.""" self.agents.append(agent) def get_available_agents_for_agent(self, agent) -> List[MicroAgent]: """Returns the list of available agents for the given purpose.""" agent_id = agent.id available_agents = [agent for agent in self.agents if agent.purpose != "Bootstrap Agent" and agent.working_agent] for agent in available_agents: if agent.parent_id != agent_id: available_agents.remove(agent) return available_agents def get_or_create_agent(self, purpose: str, depth: int, sample_input: str, force_new: bool = False, parent_agent=None) -> MicroAgent: """ Retrieves or creates an agent based on the given purpose. Optionally creates a new agent regardless of similarity if force_new is True. """ if not force_new: agent_similarity = AgentSimilarity(self.openai_wrapper, self.agents) purpose_embedding = agent_similarity.get_embedding(purpose) closest_agent, highest_similarity = agent_similarity.find_closest_agent(purpose_embedding) similarity_threshold = agent_similarity.calculate_similarity_threshold() if highest_similarity >= similarity_threshold: closest_agent.usage_count += 1 return closest_agent return self._create_and_add_agent(purpose, depth, sample_input, parent_agent=parent_agent) def _create_and_add_agent(self, purpose: str, depth: int, sample_input: str, parent_agent=None) -> MicroAgent: """Helper method to create and add a new agent.""" if len(self.agents) >= self.max_agents: self._remove_least_used_agent() new_agent = MicroAgent(self._generate_llm_prompt(purpose, sample_input), purpose, depth, self, self.openai_wrapper, parent=parent_agent) new_agent.usage_count = 1 self.agents.append(new_agent) return new_agent def _remove_least_used_agent(self): """Removes the least used agent.""" least_used_agent = min(self.agents, key=lambda agent: agent.usage_count) self.agents.remove(least_used_agent) def save_agent(self, agent: MicroAgent) -> None: """Saves the given agent with error handling.""" try: self.agent_persistence.save_agent(agent) except Exception as e: logger.exception(f"Error in saving agent: {e}") raise def remove_agent(self, agent: MicroAgent) -> None: """Removes the given agent with error handling.""" try: self.agent_persistence.remove_agent(agent) except Exception as e: logger.exception(f"Error in saving agent: {e}") raise def _generate_llm_prompt(self, goal: str, sample_input: str) -> str: """ Generates a prompt for the LLM based on the given goal and sample input. """ messages = [ {"role": "system", "content": PROMPT_ENGINEERING_SYSTEM_PROMPT}, {"role": "user", "content": PROMPT_ENGINEERING_TEMPLATE.format(goal=goal, sample_input=sample_input, examples=EXAMPLES)} ] try: return self.openai_wrapper.chat_completion(messages=messages) except<fim_suffix><fim_middle> Exception as e: logger.exception(f"Error generating LLM prompt: {e}") return ""
Exception as e: logger.exception(f"Error generating LLM prompt: {e}") return ""
CATCH
prefix_full_suffix_empty_complete_current_block_no_evidence
<filename>microagents/agents/agent_lifecycle.py<fim_prefix>import logging from typing import List from agents.microagent import MicroAgent from integrations.openaiwrapper import OpenAIAPIWrapper from agents.agent_similarity import AgentSimilarity from agents.agent_persistence_manager import AgentPersistenceManager from numpy import ndarray from prompt_management.prompts import ( PRIME_PROMPT, PRIME_NAME, PROMPT_ENGINEERING_SYSTEM_PROMPT, PROMPT_ENGINEERING_TEMPLATE, EXAMPLES ) logger = logging.getLogger() DEFAULT_MAX_AGENTS = 2000 PRIME_AGENT_WEIGHT = 25 class AgentLifecycle: def __init__(self, openai_wrapper: OpenAIAPIWrapper, agent_persistence_manager: AgentPersistenceManager, max_agents: int = DEFAULT_MAX_AGENTS): self.agents: List[MicroAgent] = [] self.openai_wrapper = openai_wrapper self.agent_persistence = agent_persistence_manager self.max_agents = max_agents def stop_all_agents(self) -> None: """Stops all agents.""" for agent in self.agents: agent.stop() def reset_all_agents(self) -> None: """Resets all agents.""" for agent in self.agents: agent.reset() def cleanup_agents(self): """Remove all agents with status stopped = True in an efficient manner.""" self.agents = [agent for agent in self.agents if not agent.stopped] def create_prime_agent(self) -> None: """Creates the prime agent and adds it to the agent list.""" prime_agent = MicroAgent( PRIME_PROMPT, PRIME_NAME, 0, self, self.openai_wrapper, PRIME_AGENT_WEIGHT, True, True ) self.agents.append(prime_agent) def add_agent(self, agent: MicroAgent) -> None: """Adds an agent to the list of agents.""" self.agents.append(agent) def get_available_agents_for_agent(self, agent) -> List[MicroAgent]: """Returns the list of available agents for the given purpose.""" agent_id = agent.id available_agents = [agent for agent in self.agents if agent.purpose != "Bootstrap Agent" and agent.working_agent] for agent in available_agents: if agent.parent_id != agent_id: available_agents.remove(agent) return available_agents def get_or_create_agent(self, purpose: str, depth: int, sample_input: str, force_new: bool = False, parent_agent=None) -> MicroAgent: """ Retrieves or creates an agent based on the given purpose. Optionally creates a new agent regardless of similarity if force_new is True. """ if not force_new: agent_similarity = AgentSimilarity(self.openai_wrapper, self.agents) purpose_embedding = agent_similarity.get_embedding(purpose) closest_agent, highest_similarity = agent_similarity.find_closest_agent(purpose_embedding) similarity_threshold = agent_similarity.calculate_similarity_threshold() if highest_similarity >= similarity_threshold: closest_agent.usage_count += 1 return closest_agent return self._create_and_add_agent(purpose, depth, sample_input, parent_agent=parent_agent) def _create_and_add_agent(self, purpose: str, depth: int, sample_input: str, parent_agent=None) -> MicroAgent: """Helper method to create and add a new agent.""" if len(self.agents) >= self.max_agents: self._remove_least_used_agent() new_agent = MicroAgent(self._generate_llm_prompt(purpose, sample_input), purpose, depth, self, self.openai_wrapper, parent=parent_agent) new_agent.usage_count = 1 self.agents.append(new_agent) return new_agent def _remove_least_used_agent(self): """Removes the least used agent.""" least_used_agent = min(self.agents, key=lambda agent: agent.usage_count) self.agents.remove(least_used_agent) def save_agent(self, agent: MicroAgent) -> None: """Saves the given agent with error handling.""" try: self.agent_persistence.save_agent(agent) except<fim_suffix><fim_middle> Exception as e: logger.exception(f"Error in saving agent: {e}") raise
Exception as e: logger.exception(f"Error in saving agent: {e}") raise
CATCH
prefix_full_suffix_empty_complete_current_block_no_evidence