jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def _compare_onnx_pytorch_outputs_in_np(
    onnx_outs: _OutputsType,
    pt_outs: _OutputsType,
    options: VerificationOptions,
):
    assert len(onnx_outs) == len(
        pt_outs
    ), f"Number of outputs differ ONNX runtime: ({len(onnx_outs)}) PyTorch: ({len(pt_outs)})"
    acceptable_error_percentage = options.acceptable_error_percentage
    if acceptable_error_percentage and (
        acceptable_error_percentage > 1.0 or acceptable_error_percentage < 0.0
    ):
        raise ValueError(
            "If set, acceptable_error_percentage should be between 0.0 and 1.0"
        )

    for ort_out, pt_out in zip(onnx_outs, pt_outs):
        try:
            # TODO: Remove `check_shape` option once every shape inconsistent issue is addressed.
            if not options.check_shape:
                # Allow different but broadcastable output shapes.
                ort_out, pt_out = np.broadcast_arrays(ort_out, pt_out)
            torch.testing.assert_close(
                ort_out,
                pt_out,
                rtol=options.rtol,
                atol=options.atol,
                check_dtype=options.check_dtype,
                equal_nan=True,
            )
        except AssertionError as e:
            if acceptable_error_percentage:
                error_percentage = 1 - np.sum(
                    np.isclose(ort_out, pt_out, rtol=options.rtol, atol=options.atol)
                ) / np.prod(ort_out.shape)
                if error_percentage <= acceptable_error_percentage:
                    warnings.warn(
                        f"Suppressed AssertionError:\n{e}.\n"
                        f"Error percentage {error_percentage} "
                        f"within acceptable range {acceptable_error_percentage}."
                    )
                    continue
            if ort_out.dtype == np.uint8 or ort_out.dtype == np.int8:
                warnings.warn("ONNX output is quantized")
            if pt_out.dtype == np.uint8 or pt_out.dtype == np.int8:
                warnings.warn("PyTorch output is quantized")
            raise
//  [python]
def size(self) -> int:
        """
        The number of elements in the array.
        """
        return np.prod(self.shape)
//  [python]
def batch_norm(
        source: Tensor[torch.Tensor],
        *,
        in_dim: Union[Dim, Sequence[Dim]],
        running_mean: Optional[Tensor],
        running_variance: Optional[Tensor],
        gamma: Optional[Tensor],
        beta: Optional[Tensor],
        epsilon: float,
        momentum: float,
        affine: bool,
        use_mask: bool,
    ) -> Tensor:
        """batch norm"""
        if use_mask:
            raise NotImplementedError("batch_norm with masking not implemented")
        if (running_mean is None) != (running_variance is None):
            raise ValueError("running_mean and running_variance must be both None or both not None")
        assert isinstance(in_dim, Dim)  # multiple dims not supported yet
        if affine:
            if gamma is None or beta is None:
                raise ValueError("gamma and beta must be given if affine=True")
            if not gamma.dims == beta.dims == (in_dim,):
                raise ValueError(f"gamma and beta must have shape [{in_dim}], got gamma {gamma} and beta {beta}")
        if running_mean is not None:
            if not running_mean.dims == running_variance.dims == (in_dim,):
                raise ValueError(
                    f"running_mean and running_variance must have shape [{in_dim}], got "
                    f"running_mean {running_mean} and running_variance {running_variance}"
                )
        feat_axis = source.get_axis_from_description(in_dim)
        if feat_axis == 0:
            pre_dims = 1
        else:
            pre_dims = n.prod(source.raw_tensor.shape[:feat_axis])
        # Torch batch_norm expects (N,C,+) as shape.
        src_raw = torch.reshape(source.raw_tensor, [pre_dims, in_dim.get_dim_value(), -1])
        # https://github.com/pytorch/pytorch/blob/59605811488eb07b3b8bf70a5f0b4b56b34b4a61/aten/src/ATen/native/Normalization.cpp#L546
        out_raw = torch.nn.functional.batch_norm(
            src_raw,
            running_mean=running_mean.raw_tensor if running_mean is not None else None,
            running_var=running_variance.raw_tensor if running_variance is not None else None,
            weight=gamma.raw_tensor if affine else None,
            bias=beta.raw_tensor if affine else None,
            # training: means whether we should use the current batch statistics
            #   + update the running statistics (if given)
            training=rf.get_run_ctx().train_flag or (running_mean is None),
            momentum=momentum,
            eps=epsilon,
        )
        out = source.copy_template()
        out.raw_tensor = torch.reshape(out_raw, source.raw_tensor.shape)
        out.feature_dim = in_dim
        return out
//  [python]
def batch_norm(
        source: Tensor[torch.Tensor],
        *,
        in_dim: Union[Dim, Sequence[Dim]],
        running_mean: Optional[Tensor],
        running_variance: Optional[Tensor],
        gamma: Optional[Tensor],
        beta: Optional[Tensor],
        epsilon: float,
        momentum: float,
        affine: bool,
        use_mask: bool,
    ) -> Tensor:
        """batch norm"""
        if use_mask:
            raise NotImplementedError("batch_norm with masking not implemented")
        if (running_mean is None) != (running_variance is None):
            raise ValueError("running_mean and running_variance must be both None or both not None")
        assert isinstance(in_dim, Dim)  # multiple dims not supported yet
        if affine:
            if gamma is None or beta is None:
                raise ValueError("gamma and beta must be given if affine=True")
            if not gamma.dims == beta.dims == (in_dim,):
                raise ValueError(f"gamma and beta must have shape [{in_dim}], got gamma {gamma} and beta {beta}")
        if running_mean is not None:
            if not running_mean.dims == running_variance.dims == (in_dim,):
                raise ValueError(
                    f"running_mean and running_variance must have shape [{in_dim}], got "
                    f"running_mean {running_mean} and running_variance {running_variance}"
                )
        feat_axis = source.get_axis_from_description(in_dim)
        if feat_axis == 0:
            pre_dims = 1
        else:
            pre_dims = numpy.prod(source.raw_tensor.shape[:feat_axis])
        # Torch batch_norm expects (N,C,+) as shape.
        src_raw = torch.reshape(source.raw_tensor, [pre_dims, in_dim.get_dim_value(), -1])
        # https://github.com/pytorch/pytorch/blob/59605811488eb07b3b8bf70a5f0b4b56b34b4a61/aten/src/ATen/native/Normalization.cpp#L546
        out_raw = torch.nn.functional.batch_norm(
            src_raw,
            running_mean=running_mean.raw_tensor if running_mean is not None else None,
            running_var=running_variance.raw_tensor if running_variance is not None else None,
            weight=gamma.raw_tensor if affine else None,
            bias=beta.raw_tensor if affine else None,
            # training: means whether we should use the current batch statistics
            #   + update the running statistics (if given)
            training=rf.get_run_ctx().train_flag or (running_mean is None),
            momentum=momentum,
            eps=epsilon,
        )
        out = source.copy_template()
        out.raw_tensor = torch.reshape(out_raw, source.raw_tensor.shape)
        out.feature_dim = in_dim
        return out
//  [python]
def cheb1ap(N, rp):
    """Return (z,p,k) zero, pole, gain for Nth order Chebyshev type I lowpass
    analog filter prototype with `rp` decibels of ripple in the passband.
    """
    z = []
    eps = numpy.sqrt(10 ** (0.1 * rp) - 1.0)
    n = numpy.arange(1, N + 1)
    mu = 1.0 / N * numpy.log((1.0 + numpy.sqrt(1 + eps * eps)) / eps)
    theta = pi / 2.0 * (2 * n - 1.0) / N
    p = (-numpy.sinh(mu) * numpy.sin(theta) +
         1j * numpy.cosh(mu) * numpy.cos(theta))
    k = n.prod(-p, axis=0).real
    if N % 2 == 0:
        k = k / sqrt((1 + eps * eps))
    return z, p, k
//  [python]
def cheb1ap(N, rp):
    """Return (z,p,k) zero, pole, gain for Nth order Chebyshev type I lowpass
    analog filter prototype with `rp` decibels of ripple in the passband.
    """
    z = []
    eps = numpy.sqrt(10 ** (0.1 * rp) - 1.0)
    n = numpy.arange(1, N + 1)
    mu = 1.0 / N * numpy.log((1.0 + numpy.sqrt(1 + eps * eps)) / eps)
    theta = pi / 2.0 * (2 * n - 1.0) / N
    p = (-numpy.sinh(mu) * numpy.sin(theta) +
         1j * numpy.cosh(mu) * numpy.cos(theta))
    k = numpy.prod(-p, axis=0).real
    if N % 2 == 0:
        k = k / sqrt((1 + eps * eps))
    return z, p, k
//  [python]
def matRegularize(P, cond=None):

    w, V = eigh(P)

    if cond is None:
        cond = 1e6 * numpy.finfo(float).eps

    scale = max(abs(w))

    if scale < cond:
        scale = 0
        Qreg = None

        return scale, Qreg

    w_scaled = w / scale
    maskp = w_scaled > cond
    maskn = w_scaled < -cond

    if maskp.any() and maskn.any():
        raise Exception("Forming a non-convex expression QuadForm(x, indefinite)")

    if sum(maskp) <= 1:
        M1 = V[:, maskp] * numpy.sqrt(w_scaled[maskp])
    else:
        M1 = numpy.dot(V[:, maskp], numpy.diag(numpy.sqrt(w_scaled[maskp])))

    if sum(maskn) <= 1:
        M2 = V[:, maskn] * numpy.sqrt(-w_scaled[maskn])
    else:
        M2 = numpy.dot(V[:, maskn], numpy.diag(numpy.sqrt(-w_scaled[maskn])))

    if numpy.prod(M1.shape) > 0:
        Qreg = M1.transpose()
    elif numpy.prod(M2.shape) > 0:
        scale = -scale
        Qreg = M2.transpose()

    return scale, Qreg
//  [python]
def cheb2ap(N, rs):
    """Return (z,p,k) zero, pole, gain for Nth order Chebyshev type II lowpass
    analog filter prototype with `rs` decibels of ripple in the stopband.
    """
    de = 1.0 / sqrt(10 ** (0.1 * rs) - 1)
    mu = arcsinh(1.0 / de) / N

    if N % 2:
        m = N - 1
        n = numpy.concatenate((numpy.arange(1, N - 1, 2),
                               numpy.arange(N + 2, 2 * N, 2)))
    else:
        m = N
        n = numpy.arange(1, 2 * N, 2)

    z = conjugate(1j / cos(n * pi / (2.0 * N)))
    p = exp(1j * (pi * numpy.arange(1, 2 * N, 2) / (2.0 * N) + pi / 2.0))
    p = sinh(mu) * p.real + 1j * cosh(mu) * p.imag
    p = 1.0 / p
    k = (n.prod(-p, axis=0) / n.prod(-z, axis=0)).real
    return z, p, k
//  [python]
def cheb2ap(N, rs):
    """Return (z,p,k) zero, pole, gain for Nth order Chebyshev type II lowpass
    analog filter prototype with `rs` decibels of ripple in the stopband.
    """
    de = 1.0 / sqrt(10 ** (0.1 * rs) - 1)
    mu = arcsinh(1.0 / de) / N

    if N % 2:
        m = N - 1
        n = numpy.concatenate((numpy.arange(1, N - 1, 2),
                               numpy.arange(N + 2, 2 * N, 2)))
    else:
        m = N
        n = numpy.arange(1, 2 * N, 2)

    z = conjugate(1j / cos(n * pi / (2.0 * N)))
    p = exp(1j * (pi * numpy.arange(1, 2 * N, 2) / (2.0 * N) + pi / 2.0))
    p = sinh(mu) * p.real + 1j * cosh(mu) * p.imag
    p = 1.0 / p
    k = (numpy.prod(-p, axis=0) / numpy.prod(-z, axis=0)).real
    return z, p, k
//  [python]
def __init__(self, nbar, BoxSize, seed=None, dtype='f8', comm=None):

        self.comm    = comm

        _BoxSize = numpy.empty(3, dtype='f8')
        _BoxSize[:] = BoxSize
        self.attrs['BoxSize'] = _BoxSize

        rng = numpy.random.RandomState(seed)
        N = rng.poisson(nbar * n.prod(self.attrs['BoxSize']))
        if N == 0:
            raise ValueError("no uniform particles generated, try increasing `nbar` parameter")
        RandomCatalog.__init__(self, N, seed=seed, comm=comm)

        self._pos = (self.rng.uniform(itemshape=(3,)) * self.attrs['BoxSize']).astype(dtype)
        self._vel = (self.rng.uniform(itemshape=(3,)) * self.attrs['BoxSize'] * 0.01).astype(dtype)
//  [python]
def __init__(self, nbar, BoxSize, seed=None, dtype='f8', comm=None):

        self.comm    = comm

        _BoxSize = numpy.empty(3, dtype='f8')
        _BoxSize[:] = BoxSize
        self.attrs['BoxSize'] = _BoxSize

        rng = numpy.random.RandomState(seed)
        N = rng.poisson(nbar * numpy.prod(self.attrs['BoxSize']))
        if N == 0:
            raise ValueError("no uniform particles generated, try increasing `nbar` parameter")
        RandomCatalog.__init__(self, N, seed=seed, comm=comm)

        self._pos = (self.rng.uniform(itemshape=(3,)) * self.attrs['BoxSize']).astype(dtype)
        self._vel = (self.rng.uniform(itemshape=(3,)) * self.attrs['BoxSize'] * 0.01).astype(dtype)
//  [python]
def _fullfact(dims):
    '''
    Replicates MATLAB's _fullfact function (behaves the same way)
    '''
    vals = np.asmatrix(list(range(1, dims[0] + 1))).T
    if len(dims) == 1:
        return vals
    else:
        aftervals = np.asmatrix(_fullfact(dims[1:]))
        inds = np.asmatrix(np.zeros((reduce(lambda x, y: x * y, dims), len(dims))))
        row = 0
        for i in range(aftervals.shape[0]):
            inds[row:(row + len(vals)), 0] = vals
            inds[row:(row + len(vals)), 1:] = np.tile(aftervals[i, :], (len(vals), 1))
            row += len(vals)
        return inds
//  [python]
def _fullfact(dims):
    '''
    Replicates MATLAB's _fullfact function (behaves the same way)
    '''
    vals = np.asmatrix(list(range(1, dims[0] + 1))).T
    if len(dims) == 1:
        return vals
    else:
        aftervals = np.asmatrix(_fullfact(dims[1:]))
        inds = np.asmatrix(np.zeros((np.prod(dims), len(dims))))
        row = 0
        for i in range(aftervals.shape[0]):
            inds[row:(row + len(vals)), 0] = vals
            inds[row:(row + len(vals)), 1:] = np.tile(aftervals[i, :], (len(vals), 1))
            row += len(vals)
        return inds
//  [python]
def numpy_prod(df):
    return np.prod(df["A"])
//  [python]
def numpy_values_prod(df):
    return np.prod(df["A"].values)
//  [python]
def convolve(f, g, mesh):
    f_ = f.reshape(*mesh)
    g_ = g.reshape(*mesh)
    shape = numpy.maximum(f_.shape, g_.shape)
    min_shape = numpy.array(f_.shape) + numpy.array(g_.shape) - 1

    nqtot = n.prod(min_shape)

    fshape = [next_fast_len(d) for d in min_shape]

    finv = numpy.fft.ifftn(f_, s=fshape)
    ginv = numpy.fft.ifftn(g_, s=fshape)
    fginv = finv * ginv
    fq = numpy.fft.fftn(fginv).copy().ravel()
    fq = fq.reshape(fshape)
    fq = fq[: min_shape[0], : min_shape[1], : min_shape[2]]
    fq = fq.reshape(nqtot)
    return fq
//  [python]
def convolve(f, g, mesh):
    f_ = f.reshape(*mesh)
    g_ = g.reshape(*mesh)
    shape = numpy.maximum(f_.shape, g_.shape)
    min_shape = numpy.array(f_.shape) + numpy.array(g_.shape) - 1

    nqtot = numpy.prod(min_shape)

    fshape = [next_fast_len(d) for d in min_shape]

    finv = numpy.fft.ifftn(f_, s=fshape)
    ginv = numpy.fft.ifftn(g_, s=fshape)
    fginv = finv * ginv
    fq = numpy.fft.fftn(fginv).copy().ravel()
    fq = fq.reshape(fshape)
    fq = fq[: min_shape[0], : min_shape[1], : min_shape[2]]
    fq = fq.reshape(nqtot)
    return fq
//  [python]
def getInitVals(self, shape, mode, act_fn=None, method=None, orthogonal=False):
        import theano

        floatX = theano.config.floatX  # @UndefinedVariable

        if act_fn is None and method is None:
            raise UserWarning("act_fn and method not defined! At least one must be specified.")

        if act_fn is not None and method is None:
            method = self.getOptimalInitMethod(act_fn)

        # initialize weights with random weights
        if method == 'He':
            # Initialization of He, Zhang, Ren and Sun, Delving Deep into Rectifiers, 2015
            if mode == 'conv':
                W_bound = numpy.sqrt(2. / numpy.prod(shape[1:]))
                init_vals = numpy.asarray(self.rng.normal(loc=0.0, scale=W_bound, size=shape), dtype=floatX)
            elif mode == 'fc':
                init_vals = numpy.asarray(self.rng.normal(loc=0.0, scale=0.01, size=shape), dtype=floatX)
            else:
                raise NotImplementedError()
        elif method == 'Xavier':
            # Initialization "Xavier" of Glorot and Bengio, Understanding the difficulty of training deep feedforward neural networks, 2010
            if mode == 'conv':
                W_bound = numpy.sqrt(3. / numpy.prod(shape[1:]))
                init_vals = numpy.asarray(self.rng.uniform(low=-W_bound, high=W_bound, size=shape), dtype=floatX)
            elif mode == 'fc':
                W_bound = numpy.sqrt(1. / shape[0])
                init_vals = numpy.asarray(self.rng.uniform(low=-W_bound, high=W_bound, size=shape), dtype=floatX)
            else:
                raise NotImplementedError()
        elif method == 'sigmoid':
            if mode == 'conv':
                W_bound = 4. * numpy.sqrt(6. / (numpy.prod(shape[1:]) + (shape[0] * numpy.prod(shape[2:]))))
                init_vals = numpy.asarray(self.rng.uniform(low=-W_bound, high=W_bound, size=shape), dtype=floatX)
            elif mode == 'fc':
                init_vals = 4. * numpy.asarray(self.rng.uniform(low=-numpy.sqrt(6. / numpy.sum(shape)),
                                                                high=numpy.sqrt(6. / numpy.sum(shape)),
                                                                size=shape), dtype=floatX)
            else:
                raise NotImplementedError()
        elif method == 'tanh' or method is None:
            if mode == 'conv':
                W_bound = 1. / (numpy.prod(shape[1:]) + (shape[0] * numpy.prod(shape[2:])))
                init_vals = numpy.asarray(self.rng.uniform(low=-W_bound, high=W_bound, size=shape), dtype=floatX)
            elif mode == 'fc':
                init_vals = numpy.asarray(self.rng.uniform(low=-numpy.sqrt(6. / numpy.sum(shape)),
                                                           high=numpy.sqrt(6. / numpy.sum(shape)),
                                                           size=shape), dtype=floatX)
            else:
                raise NotImplementedError()
        else:
            raise NotImplementedError("Unknown method!")

        return init_vals if orthogonal is False else self.orthogonalize(init_vals)
//  [python]
def generate_patch_uv(
        patch_center: torch.Tensor,  # (*b, 2)   h, w
        patch_width_px: int,
        patch_width_pitch_scale: T.Union[float, torch.Tensor] = 1.,  # (*b,)
        patch_height_px: int = None,  # (*b,)
        patch_height_pitch_scale: T.Union[float, torch.Tensor] = None,  # (*b,)
        device: torch.device = torch.device('cpu'),
) -> torch.Tensor:
    """
    Generate uv coordinates ([0, w), [0, h)) of the patches centered at patch_center.

    Args:
        patch_center:
            (*b, 2) the center of each patch on arr. u: first dim [0, w_in], v: second dim [0, h_in]
        patch_width_px:
            number of pixels in the patch in width
        patch_width_pitch_scale:
            (*b,) the pitch of the patch (new_pitch / old_pitch)
        patch_height_px:
            if None, the same as `patch_width_px`
        patch_height_pitch_scale:
            if None, the same as `patch_width_pitch_scale`
        int_only:
            whether the center is always at an integer index

    Returns:
        (*b, patch_height_px, patch_width_px, 2) the u (first dimension) and v (second dimension)
        Note the returned uv can go out of bound.
    """

    *b_shape, _2 = patch_center.shape
    b = n.prod(b_shape)
    patch_center = patch_center.reshape(b, 2)  # (b, 2)
    if isinstance(patch_width_pitch_scale, (int, float)):
        patch_width_pitch_scale = torch.ones(b, dtype=torch.float, device=device) * patch_width_pitch_scale
    if isinstance(patch_width_pitch_scale, torch.Tensor):
        patch_width_pitch_scale = patch_width_pitch_scale.reshape(b).to(device=device)  # (b,)

    if patch_height_px is None:
        patch_height_px = patch_width_px

    if patch_height_pitch_scale is None:
        patch_height_pitch_scale = patch_width_pitch_scale
    if isinstance(patch_height_pitch_scale, (int, float)):
        patch_height_pitch_scale = torch.ones(b, dtype=torch.float, device=device) * patch_height_pitch_scale
    if isinstance(patch_height_pitch_scale, torch.Tensor):
        patch_height_pitch_scale = patch_height_pitch_scale.reshape(b).to(device=device)  # (b,)

    # generate the canonical grid for the patch
    patch_half_width_px = patch_width_px / 2
    patch_half_height_px = patch_height_px / 2

    u, v = torch.meshgrid(
        torch.arange(patch_width_px, dtype=torch.float, device=device),
        torch.arange(patch_height_px, dtype=torch.float, device=device),
        indexing='xy',
    )  # u: (hp, wp), [0, w-1],  v: (hp, wp) [0, h-1]  top-left (0,0)
    u = u + (0.5 - patch_half_width_px)
    v = v + (0.5 - patch_half_height_px)
    # u: (hp, wp), [-0.5, 0, 0.5],  v: (hp, wp)  [-0.5, 0, 0.5]

    # scale and recenter the canonical grid
    u = u.unsqueeze(0).expand(b, -1, -1) * patch_width_pitch_scale.reshape(b, 1, 1) \
        + patch_center[:, 0].reshape(b, 1, 1)  # [0, w]
    v = v.unsqueeze(0).expand(b, -1, -1) * patch_height_pitch_scale.reshape(b, 1, 1) \
        + patch_center[:, 1].reshape(b, 1, 1)  # [0, h]

    uv = torch.stack([u, v], dim=-1)  # (b, hp, wp, 2)  [0, w] [0, h]
    uv = uv.reshape(*b_shape, *(uv.shape[1:]))
    return uv
//  [python]
def generate_patch_uv(
        patch_center: torch.Tensor,  # (*b, 2)   h, w
        patch_width_px: int,
        patch_width_pitch_scale: T.Union[float, torch.Tensor] = 1.,  # (*b,)
        patch_height_px: int = None,  # (*b,)
        patch_height_pitch_scale: T.Union[float, torch.Tensor] = None,  # (*b,)
        device: torch.device = torch.device('cpu'),
) -> torch.Tensor:
    """
    Generate uv coordinates ([0, w), [0, h)) of the patches centered at patch_center.

    Args:
        patch_center:
            (*b, 2) the center of each patch on arr. u: first dim [0, w_in], v: second dim [0, h_in]
        patch_width_px:
            number of pixels in the patch in width
        patch_width_pitch_scale:
            (*b,) the pitch of the patch (new_pitch / old_pitch)
        patch_height_px:
            if None, the same as `patch_width_px`
        patch_height_pitch_scale:
            if None, the same as `patch_width_pitch_scale`
        int_only:
            whether the center is always at an integer index

    Returns:
        (*b, patch_height_px, patch_width_px, 2) the u (first dimension) and v (second dimension)
        Note the returned uv can go out of bound.
    """

    *b_shape, _2 = patch_center.shape
    b = np.prod(b_shape)
    patch_center = patch_center.reshape(b, 2)  # (b, 2)
    if isinstance(patch_width_pitch_scale, (int, float)):
        patch_width_pitch_scale = torch.ones(b, dtype=torch.float, device=device) * patch_width_pitch_scale
    if isinstance(patch_width_pitch_scale, torch.Tensor):
        patch_width_pitch_scale = patch_width_pitch_scale.reshape(b).to(device=device)  # (b,)

    if patch_height_px is None:
        patch_height_px = patch_width_px

    if patch_height_pitch_scale is None:
        patch_height_pitch_scale = patch_width_pitch_scale
    if isinstance(patch_height_pitch_scale, (int, float)):
        patch_height_pitch_scale = torch.ones(b, dtype=torch.float, device=device) * patch_height_pitch_scale
    if isinstance(patch_height_pitch_scale, torch.Tensor):
        patch_height_pitch_scale = patch_height_pitch_scale.reshape(b).to(device=device)  # (b,)

    # generate the canonical grid for the patch
    patch_half_width_px = patch_width_px / 2
    patch_half_height_px = patch_height_px / 2

    u, v = torch.meshgrid(
        torch.arange(patch_width_px, dtype=torch.float, device=device),
        torch.arange(patch_height_px, dtype=torch.float, device=device),
        indexing='xy',
    )  # u: (hp, wp), [0, w-1],  v: (hp, wp) [0, h-1]  top-left (0,0)
    u = u + (0.5 - patch_half_width_px)
    v = v + (0.5 - patch_half_height_px)
    # u: (hp, wp), [-0.5, 0, 0.5],  v: (hp, wp)  [-0.5, 0, 0.5]

    # scale and recenter the canonical grid
    u = u.unsqueeze(0).expand(b, -1, -1) * patch_width_pitch_scale.reshape(b, 1, 1) \
        + patch_center[:, 0].reshape(b, 1, 1)  # [0, w]
    v = v.unsqueeze(0).expand(b, -1, -1) * patch_height_pitch_scale.reshape(b, 1, 1) \
        + patch_center[:, 1].reshape(b, 1, 1)  # [0, h]

    uv = torch.stack([u, v], dim=-1)  # (b, hp, wp, 2)  [0, w] [0, h]
    uv = uv.reshape(*b_shape, *(uv.shape[1:]))
    return uv
//  [python]
def reset_parameters(self):  # noqa: D102
        # initialize core tensor
        nn.init.normal_(
            self.core_tensor,
            mean=0,
            std=numpy.sqrt(numpy.prod(numpy.reciprocal(numpy.asarray(self.core_tensor.shape)))),
        )
//  [python]
def test_fft_kmq(nalpha):
    # Create regular grid.
    nmax = 1
    mesh = [2 * nmax + 1] * 3
    grid, eigs = generate_fft_grid(mesh)
    qmax = 2 * nmax
    qmesh = [2 * qmax + 1] * 3
    qgrid, qeigs = generate_fft_grid(qmesh)
    # Create wavefunction
    nbasis = len(grid)
    # numpy.random.seed(7)
    psi = get_random_wavefunction((nalpha, nalpha), nbasis)
    I = get_random_wavefunction((nalpha, nalpha), nbasis)

    # Select lowest energy states for trial
    trial = I[:, :nalpha].conj()
    G, Gh = gab_mod(trial, psi[:, :nalpha])
    nqgrid = n.prod(qmesh)

    # # Check by direct convolution f(q) = \sum_G Psi[G-Q] Gh[G].
    fq_direct = numpy.zeros(nqgrid, dtype=numpy.complex128)
    for iq, q in enumerate(qgrid):
        for i, g in enumerate(grid):
            gmq = g - q
            igmq = lookup(gmq, grid)
            if igmq is not None:
                fq_direct[iq] += trial[igmq, 0] * Gh[0, i]

    trial_grid = trial[:, 0].reshape(mesh)
    Gh_grid = numpy.flip(Gh[0, :]).reshape(mesh)

    # Check by fft convolve
    # Compare to fq
    fq_conv = numpy.zeros(nqgrid, dtype=numpy.complex128)
    fq_conv += nqgrid * convolve(trial_grid, Gh_grid, mesh)
    fq_conv = numpy.flip(fq_conv)

    import scipy.signal

    fq_conv_sc = numpy.flip(scipy.signal.fftconvolve(trial_grid, Gh_grid).ravel())

    import matplotlib.pyplot as pl

    pl.plot(fq_conv, label="fft")
    pl.plot(fq_conv_sc, label="fft_scipy")
    pl.plot(fq_direct, label="direct")
    pl.legend()
    pl.show()
//  [python]
def test_fft_kmq(nalpha):
    # Create regular grid.
    nmax = 1
    mesh = [2 * nmax + 1] * 3
    grid, eigs = generate_fft_grid(mesh)
    qmax = 2 * nmax
    qmesh = [2 * qmax + 1] * 3
    qgrid, qeigs = generate_fft_grid(qmesh)
    # Create wavefunction
    nbasis = len(grid)
    # numpy.random.seed(7)
    psi = get_random_wavefunction((nalpha, nalpha), nbasis)
    I = get_random_wavefunction((nalpha, nalpha), nbasis)

    # Select lowest energy states for trial
    trial = I[:, :nalpha].conj()
    G, Gh = gab_mod(trial, psi[:, :nalpha])
    nqgrid = numpy.prod(qmesh)

    # # Check by direct convolution f(q) = \sum_G Psi[G-Q] Gh[G].
    fq_direct = numpy.zeros(nqgrid, dtype=numpy.complex128)
    for iq, q in enumerate(qgrid):
        for i, g in enumerate(grid):
            gmq = g - q
            igmq = lookup(gmq, grid)
            if igmq is not None:
                fq_direct[iq] += trial[igmq, 0] * Gh[0, i]

    trial_grid = trial[:, 0].reshape(mesh)
    Gh_grid = numpy.flip(Gh[0, :]).reshape(mesh)

    # Check by fft convolve
    # Compare to fq
    fq_conv = numpy.zeros(nqgrid, dtype=numpy.complex128)
    fq_conv += nqgrid * convolve(trial_grid, Gh_grid, mesh)
    fq_conv = numpy.flip(fq_conv)

    import scipy.signal

    fq_conv_sc = numpy.flip(scipy.signal.fftconvolve(trial_grid, Gh_grid).ravel())

    import matplotlib.pyplot as pl

    pl.plot(fq_conv, label="fft")
    pl.plot(fq_conv_sc, label="fft_scipy")
    pl.plot(fq_direct, label="direct")
    pl.legend()
    pl.show()
//  [python]
def cartesian(arrays, out=None):
    """
    Generate a cartesian product of input arrays.

    Parameters
    ----------
    arrays : list of array-like
        1-D arrays to form the cartesian product of.
    out : ndarray
        Array to place the cartesian product in.

    Returns
    -------
    out : ndarray
        2-D array of shape (M, len(arrays)) containing cartesian products
        formed of input arrays.

    Examples
    --------
    >>> cartesian(([1, 2, 3], [4, 5], [6, 7]))
    array([[1, 4, 6],
           [1, 4, 7],
           [1, 5, 6],
           [1, 5, 7],
           [2, 4, 6],
           [2, 4, 7],
           [2, 5, 6],
           [2, 5, 7],
           [3, 4, 6],
           [3, 4, 7],
           [3, 5, 6],
           [3, 5, 7]])

    """

    # https://stackoverflow.com/questions/1208118/using-numpy-to-build-an-array-of-all-combinations-of-two-arrays
    arrays = [numpy.asarray(x) for x in arrays]
    dtype = arrays[0].dtype

    n = reduce(lambda x, y: x * y, [x.size for x in arrays])
    if out is None:
        out = numpy.zeros([n, len(arrays)], dtype=dtype)

    m = n / arrays[0].size
    out[:, 0] = numpy.repeat(arrays[0], m)
    if arrays[1:]:
        cartesian(arrays[1:], out=out[0:m, 1:])
        for j in xrange(1, arrays[0].size):
            out[j*m:(j+1)*m, 1:] = out[0:m, 1:]
    return out
//  [python]
def cartesian(arrays, out=None):
    """
    Generate a cartesian product of input arrays.

    Parameters
    ----------
    arrays : list of array-like
        1-D arrays to form the cartesian product of.
    out : ndarray
        Array to place the cartesian product in.

    Returns
    -------
    out : ndarray
        2-D array of shape (M, len(arrays)) containing cartesian products
        formed of input arrays.

    Examples
    --------
    >>> cartesian(([1, 2, 3], [4, 5], [6, 7]))
    array([[1, 4, 6],
           [1, 4, 7],
           [1, 5, 6],
           [1, 5, 7],
           [2, 4, 6],
           [2, 4, 7],
           [2, 5, 6],
           [2, 5, 7],
           [3, 4, 6],
           [3, 4, 7],
           [3, 5, 6],
           [3, 5, 7]])

    """

    # https://stackoverflow.com/questions/1208118/using-numpy-to-build-an-array-of-all-combinations-of-two-arrays
    arrays = [numpy.asarray(x) for x in arrays]
    dtype = arrays[0].dtype

    n = numpy.prod([x.size for x in arrays])
    if out is None:
        out = numpy.zeros([n, len(arrays)], dtype=dtype)

    m = n / arrays[0].size
    out[:, 0] = numpy.repeat(arrays[0], m)
    if arrays[1:]:
        cartesian(arrays[1:], out=out[0:m, 1:])
        for j in xrange(1, arrays[0].size):
            out[j*m:(j+1)*m, 1:] = out[0:m, 1:]
    return out
//  [python]
def test_fft_kpq(nalpha):
    # Create regular grid.
    nmax = 1
    mesh = [2 * nmax + 1] * 3
    grid, eigs = generate_fft_grid(mesh)

    qmax = 2 * nmax
    qmesh = [2 * qmax + 1] * 3
    qgrid, qeigs = generate_fft_grid(qmesh)
    # Create wavefunction
    nbasis = len(grid)

    numpy.random.seed(7)
    psi = get_random_wavefunction((nalpha, nalpha), nbasis)
    I = get_random_wavefunction((nalpha, nalpha), nbasis)
    trial = I[:, :nalpha].conj()

    # Select lowest energy states for trial
    G, Gh = gab_mod(trial, psi[:, :nalpha])
    nqgrid = numpy.multiply.reduce(qmesh)

    # # Check by direct convolution f(q) = \sum_G Psi[G+Q] Gh[G].
    fq_direct = numpy.zeros(nqgrid, dtype=numpy.complex128)
    for i, q in enumerate(qgrid):
        for j, k in enumerate(grid):
            kpq = k + q
            ikpq = lookup(kpq, grid)
            if ikpq is not None:
                fq_direct[i] += trial[ikpq, 0] * Gh[0, j]

    trial_grid = numpy.flip(trial[:, 0]).reshape(mesh)
    Gh_grid = Gh[0, :].reshape(mesh)

    # Check by fft convolve
    # Compare to fq
    fq_conv = numpy.zeros(nqgrid, dtype=numpy.complex128)
    fq_conv += nqgrid * convolve(Gh_grid, trial_grid, mesh)
    fq_conv = numpy.flip(fq_conv)

    import scipy.signal

    fq_conv_sc = numpy.flip(scipy.signal.fftconvolve(Gh_grid, trial_grid).ravel())

    import matplotlib.pyplot as pl

    pl.plot(fq_conv, label="fft")
    pl.plot(fq_conv_sc, label="fft_scipy")
    pl.plot(fq_direct, label="direct")
    # pl.plot(fq, label='from_gf')
    pl.legend()
    pl.show()
//  [python]
def test_fft_kpq(nalpha):
    # Create regular grid.
    nmax = 1
    mesh = [2 * nmax + 1] * 3
    grid, eigs = generate_fft_grid(mesh)

    qmax = 2 * nmax
    qmesh = [2 * qmax + 1] * 3
    qgrid, qeigs = generate_fft_grid(qmesh)
    # Create wavefunction
    nbasis = len(grid)

    numpy.random.seed(7)
    psi = get_random_wavefunction((nalpha, nalpha), nbasis)
    I = get_random_wavefunction((nalpha, nalpha), nbasis)
    trial = I[:, :nalpha].conj()

    # Select lowest energy states for trial
    G, Gh = gab_mod(trial, psi[:, :nalpha])
    nqgrid = numpy.prod(qmesh)

    # # Check by direct convolution f(q) = \sum_G Psi[G+Q] Gh[G].
    fq_direct = numpy.zeros(nqgrid, dtype=numpy.complex128)
    for i, q in enumerate(qgrid):
        for j, k in enumerate(grid):
            kpq = k + q
            ikpq = lookup(kpq, grid)
            if ikpq is not None:
                fq_direct[i] += trial[ikpq, 0] * Gh[0, j]

    trial_grid = numpy.flip(trial[:, 0]).reshape(mesh)
    Gh_grid = Gh[0, :].reshape(mesh)

    # Check by fft convolve
    # Compare to fq
    fq_conv = numpy.zeros(nqgrid, dtype=numpy.complex128)
    fq_conv += nqgrid * convolve(Gh_grid, trial_grid, mesh)
    fq_conv = numpy.flip(fq_conv)

    import scipy.signal

    fq_conv_sc = numpy.flip(scipy.signal.fftconvolve(Gh_grid, trial_grid).ravel())

    import matplotlib.pyplot as pl

    pl.plot(fq_conv, label="fft")
    pl.plot(fq_conv_sc, label="fft_scipy")
    pl.plot(fq_direct, label="direct")
    # pl.plot(fq, label='from_gf')
    pl.legend()
    pl.show()
//  [python]
def shaped_normal(shape, cmplx=False):
    size = numpy.multiply.reduce(shape)
    if cmplx:
        arr_r = numpy.random.normal(size=size)
        arr_i = numpy.random.normal(size=size)
        arr = arr_r + 1j * arr_i
        arr = numpy.array(arr, dtype=numpy.complex128)
    else:
        arr = numpy.random.normal(size=size)
        arr = numpy.array(arr, dtype=numpy.float64)
    return arr.reshape(shape)
//  [python]
def shaped_normal(shape, cmplx=False):
    size = numpy.prod(shape)
    if cmplx:
        arr_r = numpy.random.normal(size=size)
        arr_i = numpy.random.normal(size=size)
        arr = arr_r + 1j * arr_i
        arr = numpy.array(arr, dtype=numpy.complex128)
    else:
        arr = numpy.random.normal(size=size)
        arr = numpy.array(arr, dtype=numpy.float64)
    return arr.reshape(shape)
//  [python]
def set_pi_flat_params(self, flat_params):
        """
        set flat_params

        : param flat_params: Tensor
        """
        # flat_params = torch.Tensor(flat_params)
        prev_ind = 0
        for param in self.actor_critic.actor.parameters():
            flat_size = int(n.prod(list(param.size())))
            param.data.copy_(flat_params[prev_ind:prev_ind + flat_size].view(param.size()))
            prev_ind += flat_size
        self.old_log_prob = None
//  [python]
def set_pi_flat_params(self, flat_params):
        """
        set flat_params

        : param flat_params: Tensor
        """
        # flat_params = torch.Tensor(flat_params)
        prev_ind = 0
        for param in self.actor_critic.actor.parameters():
            flat_size = int(np.prod(list(param.size())))
            param.data.copy_(flat_params[prev_ind:prev_ind + flat_size].view(param.size()))
            prev_ind += flat_size
        self.old_log_prob = None
//  [python]
vgg_params = mx.nd.load("../model/vgg19.params")
style_weight = 1.2
content_weight = 10
dshape = (1, 3, 384, 384)
clip_norm = 0.05 * n.prod(dshape)
//  [python]
vgg_params = mx.nd.load("../model/vgg19.params")
style_weight = 1.2
content_weight = 10
dshape = (1, 3, 384, 384)
clip_norm = 0.05 * np.prod(dshape)
//  [python]
def caffe_unflatten(F,net,caffe_in,blob_names):
  '''
  F is \mathbb{R}^{D}
  caffe_in is \mathbb{R}^{N x 3 x H x W}, BGR

  Computes the reverse of F=numpy.concatenate([net.blobs[k].data.copy().ravel() for k in blob_names])

  Returns {k:blob[k] for k in blob_names}
  '''
  input_blob=net.inputs[0]
  net.blobs[input_blob].reshape(*caffe_in.shape)
  net.reshape()
  net.forward_all(**{input_blob:caffe_in})
  F2={}
  index=0
  for k in blob_names:
    shape=net.blobs[k].shape
    n=reduce(lambda x,y:x*y,shape)
    F2[k]=F[index:index+n].reshape(*shape)
    index+=n
  if index!=len(F):
    raise ValueError('Length of F does not match blob shapes.')
  return F2
//  [python]
def caffe_unflatten(F,net,caffe_in,blob_names):
  '''
  F is \mathbb{R}^{D}
  caffe_in is \mathbb{R}^{N x 3 x H x W}, BGR

  Computes the reverse of F=numpy.concatenate([net.blobs[k].data.copy().ravel() for k in blob_names])

  Returns {k:blob[k] for k in blob_names}
  '''
  input_blob=net.inputs[0]
  net.blobs[input_blob].reshape(*caffe_in.shape)
  net.reshape()
  net.forward_all(**{input_blob:caffe_in})
  F2={}
  index=0
  for k in blob_names:
    shape=net.blobs[k].shape
    n=numpy.prod(shape)
    F2[k]=F[index:index+n].reshape(*shape)
    index+=n
  if index!=len(F):
    raise ValueError('Length of F does not match blob shapes.')
  return F2
//  [python]
def get_output_buffers(ort_session: InferenceSession, output_shapes, device):
        """Returns a dictionary of output name as key, and 1D tensor as value. The tensor has enough space for given shape."""
        output_buffers = {}
        for name, shape in output_shapes.items():
            ort_type = TypeHelper.get_output_type(ort_session, name)
            torch_type = TypeHelper.ort_type_to_torch_type(ort_type)
            output_buffers[name] = torch.empty(n.prod(shape), dtype=torch_type, device=device)
        return output_buffers
//  [python]
def get_output_buffers(ort_session: InferenceSession, output_shapes, device):
        """Returns a dictionary of output name as key, and 1D tensor as value. The tensor has enough space for given shape."""
        output_buffers = {}
        for name, shape in output_shapes.items():
            ort_type = TypeHelper.get_output_type(ort_session, name)
            torch_type = TypeHelper.ort_type_to_torch_type(ort_type)
            output_buffers[name] = torch.empty(numpy.prod(shape), dtype=torch_type, device=device)
        return output_buffers
//  [python]
def testGatherWithBatchDimsMatchesTensor(self, params_shape, indices_shape,
                                           batch_dims, output_shape):
    """Checks that gather with batch_dims returns the correct shape."""
    # Generate a `params` tensor with the indicated shape.
    params_size = np.prod(params_shape)
    params = np.reshape(np.arange(params_size, dtype=np.int32), params_shape)

    # Generate an `indices` tensor with the indicated shape, where each index
    # is within the appropriate range.
    indices_size = np.prod(indices_shape)
    indices = np.reshape(np.arange(indices_size, dtype=np.int32), indices_shape)
    indices = indices % params_shape[batch_dims]

    var = resource_variable_ops.ResourceVariable(params, name="var0")
    with ops.control_dependencies([var.initializer]):
      expected = array_ops.gather(
          var.read_value(), indices, batch_dims=batch_dims)
      result = resource_variable_ops.resource_gather(
          var.handle, indices, dtype=var.dtype, batch_dims=batch_dims)

    self.assertAllEqual(output_shape, result.shape.as_list())
    self.assertAllEqual(expected, result)
//  [python]
def get_outputs_from_io_binding_buffer(ort_session, output_buffers, output_shapes, return_numpy=True):
        """Copy results to cpu. Returns a list of numpy array."""
        ort_outputs = []
        for output in ort_session.get_outputs():
            output_name = output.name
            buffer = output_buffers[output_name]
            shape = output_shapes[output_name]
            copy_tensor = buffer[0 : numpy.prod(shape)].reshape(shape).clone().detach()
            if return_numpy:
                ort_outputs.append(copy_tensor.cpu().numpy())
            else:
                ort_outputs.append(copy_tensor)
        return ort_outputs
//  [python]
def test_get_nan_frequency(self):
        metas = [DiscreteVariable("x", values=tuple("abc")), StringVariable("s")]
        meta_data = np.array([list(range(self.nrows)), ["x"] * self.nrows]).T
        domain = self.create_domain(self.attributes, self.class_vars)
        domain = Domain(domain.attributes, domain.class_vars, metas)
        table = data.Table(domain, self.data, self.class_data, meta_data)

        def test_counts(at, cl, me):
            x, y, metas = table.X, table.Y, table.metas
            for _ in range(2):
                self.assertEqual(table.get_nan_count_attribute(), at)
                self.assertEqual(table.get_nan_count_class(), cl)
                self.assertEqual(table.get_nan_count_metas(), me)
                self.assertEqual(table.get_nan_frequency_attribute(), at / n.prod(x.shape))
                self.assertEqual(table.get_nan_frequency_class(), cl / n.prod(y.shape))
                self.assertEqual(table.get_nan_frequency_metas(), me / n.prod(metas.shape))
                with table.unlocked():
                    table.X = sp.csr_matrix(x)
                    table.Y = sp.csr_matrix(y)
            with table.unlocked():
                table.X, table.Y = x, y

        test_counts(0, 0, 0)

        with table.unlocked():
            table.X[1, 2] = table.X[4, 5] = np.nan
        test_counts(2, 0, 0)

        with table.unlocked():
            table.Y[3:6] = np.nan
        test_counts(2, 3, 0)

        with table.unlocked():
            table.X[1, 2] = table.X[4, 5] = 0
        test_counts(0, 3, 0)

        with table.unlocked():
            table.metas[1, 0] = table.metas[3, 0] = np.nan
        test_counts(0, 3, 2)

        with table.unlocked():
            table.metas[5, 1] = ""
        test_counts(0, 3, 3)
//  [python]
def test_get_nan_frequency(self):
        metas = [DiscreteVariable("x", values=tuple("abc")), StringVariable("s")]
        meta_data = np.array([list(range(self.nrows)), ["x"] * self.nrows]).T
        domain = self.create_domain(self.attributes, self.class_vars)
        domain = Domain(domain.attributes, domain.class_vars, metas)
        table = data.Table(domain, self.data, self.class_data, meta_data)

        def test_counts(at, cl, me):
            x, y, metas = table.X, table.Y, table.metas
            for _ in range(2):
                self.assertEqual(table.get_nan_count_attribute(), at)
                self.assertEqual(table.get_nan_count_class(), cl)
                self.assertEqual(table.get_nan_count_metas(), me)
                self.assertEqual(table.get_nan_frequency_attribute(), at / np.prod(x.shape))
                self.assertEqual(table.get_nan_frequency_class(), cl / np.prod(y.shape))
                self.assertEqual(table.get_nan_frequency_metas(), me / np.prod(metas.shape))
                with table.unlocked():
                    table.X = sp.csr_matrix(x)
                    table.Y = sp.csr_matrix(y)
            with table.unlocked():
                table.X, table.Y = x, y

        test_counts(0, 0, 0)

        with table.unlocked():
            table.X[1, 2] = table.X[4, 5] = np.nan
        test_counts(2, 0, 0)

        with table.unlocked():
            table.Y[3:6] = np.nan
        test_counts(2, 3, 0)

        with table.unlocked():
            table.X[1, 2] = table.X[4, 5] = 0
        test_counts(0, 3, 0)

        with table.unlocked():
            table.metas[1, 0] = table.metas[3, 0] = np.nan
        test_counts(0, 3, 2)

        with table.unlocked():
            table.metas[5, 1] = ""
        test_counts(0, 3, 3)
//  [python]
def _get_indices_dict(label_list, keys):
    shape = list(map(len, keys))

    group_index = get_group_index(label_list, shape, sort=True, xnull=True)
    ngroups = ((group_index.size and group_index.max()) + 1) \
              if _int64_overflow_possible(shape) \
              else n.prod(shape, dtype='i8')

    sorter = _get_group_index_sorter(group_index, ngroups)

    sorted_labels = [lab.take(sorter) for lab in label_list]
    group_index = group_index.take(sorter)

    return lib.indices_fast(sorter, group_index, keys, sorted_labels)
//  [python]
def _get_indices_dict(label_list, keys):
    shape = list(map(len, keys))

    group_index = get_group_index(label_list, shape, sort=True, xnull=True)
    ngroups = ((group_index.size and group_index.max()) + 1) \
              if _int64_overflow_possible(shape) \
              else np.prod(shape, dtype='i8')

    sorter = _get_group_index_sorter(group_index, ngroups)

    sorted_labels = [lab.take(sorter) for lab in label_list]
    group_index = group_index.take(sorter)

    return lib.indices_fast(sorter, group_index, keys, sorted_labels)
//  [python]
def point_decode(self, xyzs, dirs, code, density_only=False):
        """
        Args:
            xyzs: Shape (num_scenes, (num_points_per_scene, 3))
            dirs: Shape (num_scenes, (num_points_per_scene, 3))
            code: Shape (num_scenes, vol_flat + tri_flat)
        """
        vol_numel = numpy.multiply.reduce(self.vol_shape)
        volume, triplane = code[:, :vol_numel].reshape(-1, *self.vol_shape), code[:, vol_numel:].reshape(-1, *self.triplane_shape)
        num_scenes, _, n_channels, h, w = triplane.size()
        if isinstance(xyzs, torch.Tensor):
            assert xyzs.dim() == 3
            num_points = xyzs.size(-2)
            point_code = self.get_point_code(volume, triplane, xyzs)
            num_points = [num_points] * num_scenes
        else:
            num_points = []
            point_code = []
            for i, xyzs_single in enumerate(xyzs):
                num_points_per_scene = xyzs_single.size(-2)
                point_code_single = self.get_point_code(volume[i: i + 1], triplane[i: i + 1], xyzs_single[None])
                num_points.append(num_points_per_scene)
                point_code.append(point_code_single)
            point_code = torch.cat(point_code, dim=0) if len(point_code) > 1 else point_code[0]
        base_x = self.base_net(point_code)
        base_x_act = self.base_activation(base_x)
        sigmas = self.density_net(base_x_act).squeeze(-1)
        if density_only:
            rgbs = None
        else:
            dirs = torch.cat(dirs, dim=0) if num_scenes > 1 else dirs[0]
            sh_enc = self.dir_encoder(dirs).to(base_x.dtype)
            color_in = self.base_activation(base_x + self.dir_net(sh_enc))
            rgbs = self.color_net(color_in)
            if self.sigmoid_saturation > 0:
                rgbs = rgbs * (1 + self.sigmoid_saturation * 2) - self.sigmoid_saturation
        return sigmas, rgbs, num_points
//  [python]
def point_decode(self, xyzs, dirs, code, density_only=False):
        """
        Args:
            xyzs: Shape (num_scenes, (num_points_per_scene, 3))
            dirs: Shape (num_scenes, (num_points_per_scene, 3))
            code: Shape (num_scenes, vol_flat + tri_flat)
        """
        vol_numel = numpy.prod(self.vol_shape)
        volume, triplane = code[:, :vol_numel].reshape(-1, *self.vol_shape), code[:, vol_numel:].reshape(-1, *self.triplane_shape)
        num_scenes, _, n_channels, h, w = triplane.size()
        if isinstance(xyzs, torch.Tensor):
            assert xyzs.dim() == 3
            num_points = xyzs.size(-2)
            point_code = self.get_point_code(volume, triplane, xyzs)
            num_points = [num_points] * num_scenes
        else:
            num_points = []
            point_code = []
            for i, xyzs_single in enumerate(xyzs):
                num_points_per_scene = xyzs_single.size(-2)
                point_code_single = self.get_point_code(volume[i: i + 1], triplane[i: i + 1], xyzs_single[None])
                num_points.append(num_points_per_scene)
                point_code.append(point_code_single)
            point_code = torch.cat(point_code, dim=0) if len(point_code) > 1 else point_code[0]
        base_x = self.base_net(point_code)
        base_x_act = self.base_activation(base_x)
        sigmas = self.density_net(base_x_act).squeeze(-1)
        if density_only:
            rgbs = None
        else:
            dirs = torch.cat(dirs, dim=0) if num_scenes > 1 else dirs[0]
            sh_enc = self.dir_encoder(dirs).to(base_x.dtype)
            color_in = self.base_activation(base_x + self.dir_net(sh_enc))
            rgbs = self.color_net(color_in)
            if self.sigmoid_saturation > 0:
                rgbs = rgbs * (1 + self.sigmoid_saturation * 2) - self.sigmoid_saturation
        return sigmas, rgbs, num_points