jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def __getitem__(self, pos):
        if self.stackSize and self.fName:
            if pos < 0 or pos >= self.stackSize[0]:
                raise IndexError("0 <= pos <= %i, but pos = %i" % (self.stackSize[0] - 1, pos))

            pos = max(0, min(pos, self.stackSize[0] - 1))
            voxels = reduce(lambda x, y: x*y, self.stackSize[1:])
            # use int64 for bigger files
            offset = np.int64(2) * pos * voxels

            with open(os.path.join(self.fName, "data/data.bin"), "rb") as f:
                f.seek(offset)
                return np.fromfile(f, dtype="<u2",
                                   count=voxels).reshape(self.stackSize[1:])
        else:
            return None
//  [python]
def __getitem__(self, pos):
        if self.stackSize and self.fName:
            if pos < 0 or pos >= self.stackSize[0]:
                raise IndexError("0 <= pos <= %i, but pos = %i" % (self.stackSize[0] - 1, pos))

            pos = max(0, min(pos, self.stackSize[0] - 1))
            voxels = np.prod(self.stackSize[1:])
            # use int64 for bigger files
            offset = np.int64(2) * pos * voxels

            with open(os.path.join(self.fName, "data/data.bin"), "rb") as f:
                f.seek(offset)
                return np.fromfile(f, dtype="<u2",
                                   count=voxels).reshape(self.stackSize[1:])
        else:
            return None
//  [python]
def pruning_loss_sens_approx(
    input_shape: Union[None, List[int], List[List[int]]],
    output_shape: Union[None, List[int]],
    params: int,
    apply_shape_change_mult: bool = True,
) -> float:
    """
    Approximate the pruning sensitivity of a Neural Network's layer
    based on the params and metadata for a given layer

    :param input_shape: the input shape to the layer
    :param output_shape: the output shape from the layer
    :param params: the number of params in the layer
    :param apply_shape_change_mult: True to adjust the sensitivity based on
        a weight derived from a change in input to output shape
        (any change is considered to be more sensitive), False to not apply
    :return: the approximated pruning sensitivity for the layer's settings
    """
    if not params:
        return 0.0

    if input_shape:
        input_shape = flatten_iterable(input_shape)
        input_shape = [
            size for size in input_shape if size and isinstance(size, numbers.Number)
        ]

    input_volume = 0 if not input_shape else n.prod(input_shape).item()

    if output_shape:
        output_shape = flatten_iterable(output_shape)
        output_shape = [
            size for size in output_shape if size and isinstance(size, numbers.Number)
        ]

    output_volume = 0 if not output_shape else n.prod(output_shape).item()
    total_volume = input_volume + output_volume

    features_per_params = total_volume / float(params)
    shape_change_mult = (
        1.0
        if not apply_shape_change_mult or not input_volume or not output_volume
        else max(input_volume / output_volume, output_volume / input_volume)
    )

    return features_per_params * shape_change_mult
//  [python]
def pruning_loss_sens_approx(
    input_shape: Union[None, List[int], List[List[int]]],
    output_shape: Union[None, List[int]],
    params: int,
    apply_shape_change_mult: bool = True,
) -> float:
    """
    Approximate the pruning sensitivity of a Neural Network's layer
    based on the params and metadata for a given layer

    :param input_shape: the input shape to the layer
    :param output_shape: the output shape from the layer
    :param params: the number of params in the layer
    :param apply_shape_change_mult: True to adjust the sensitivity based on
        a weight derived from a change in input to output shape
        (any change is considered to be more sensitive), False to not apply
    :return: the approximated pruning sensitivity for the layer's settings
    """
    if not params:
        return 0.0

    if input_shape:
        input_shape = flatten_iterable(input_shape)
        input_shape = [
            size for size in input_shape if size and isinstance(size, numbers.Number)
        ]

    input_volume = 0 if not input_shape else numpy.prod(input_shape).item()

    if output_shape:
        output_shape = flatten_iterable(output_shape)
        output_shape = [
            size for size in output_shape if size and isinstance(size, numbers.Number)
        ]

    output_volume = 0 if not output_shape else numpy.prod(output_shape).item()
    total_volume = input_volume + output_volume

    features_per_params = total_volume / float(params)
    shape_change_mult = (
        1.0
        if not apply_shape_change_mult or not input_volume or not output_volume
        else max(input_volume / output_volume, output_volume / input_volume)
    )

    return features_per_params * shape_change_mult
//  [python]
def ncov(shape,wcs,noise_uk_arcmin):
    noise_uK_rad = noise_uk_arcmin*np.pi/180./60.
    normfact = np.sqrt(n.prod(enmap.pixsize(shape[-2:],wcs)))
    noise_uK_pixel = noise_uK_rad/normfact
    return np.diag([(noise_uK_pixel)**2.]*n.prod(shape[-2:]))
//  [python]
def ncov(shape,wcs,noise_uk_arcmin):
    noise_uK_rad = noise_uk_arcmin*np.pi/180./60.
    normfact = np.sqrt(np.prod(enmap.pixsize(shape[-2:],wcs)))
    noise_uK_pixel = noise_uK_rad/normfact
    return np.diag([(noise_uK_pixel)**2.]*np.prod(shape[-2:]))
//  [python]
def __init__(self,shape,wcs,iau=False):
        """Initialize with a geometry shape and wcs."""
        
        self.shape = shape
        self.wcs = wcs
        self.normfact = enmap.area(self.shape,self.wcs )/ n.prod(self.shape[-2:])**2.         
        if len(shape) > 2 and shape[-3] > 1:
            self.rot = enmap.queb_rotmat(enmap.lmap(shape,wcs),iau=iau)
//  [python]
def __init__(self,shape,wcs,iau=False):
        """Initialize with a geometry shape and wcs."""
        
        self.shape = shape
        self.wcs = wcs
        self.normfact = enmap.area(self.shape,self.wcs )/ np.prod(self.shape[-2:])**2.         
        if len(shape) > 2 and shape[-3] > 1:
            self.rot = enmap.queb_rotmat(enmap.lmap(shape,wcs),iau=iau)
//  [python]
def __init__(self,shape,wcs,template=None,noise_power=None):
        shape = shape[-2:]
        area = enmap.area(shape,wcs)
        self.normfact = area / (n.prod(shape))**2
        if noise_power is not None: self.n2d = noise_power
        if template is not None: self.ktemp = enmap.fft(template,normalize=False)
//  [python]
def __init__(self,shape,wcs,template=None,noise_power=None):
        shape = shape[-2:]
        area = enmap.area(shape,wcs)
        self.normfact = area / (np.prod(shape))**2
        if noise_power is not None: self.n2d = noise_power
        if template is not None: self.ktemp = enmap.fft(template,normalize=False)
//  [python]
def __init__(self,shape,wcs,cov=None,covsqrt=None,pixel_units=False,smooth="auto",ndown=None,order=1):
                self.shape = shape
                self.wcs = wcs
                assert cov.ndim>=3 , "Power spectra have to be of shape (ncomp,ncomp,lmax) or (ncomp,ncomp,Ny,Nx)."
                if covsqrt is not None:
                    self.covsqrt = covsqrt
                else:
                    if cov.ndim==4:
                            if not(pixel_units): cov = cov * n.prod(shape[-2:])/enmap.area(shape,wcs )
                            if ndown:
                                self.covsqrt = downsample_power(shape,wcs,cov,ndown,order,exp=0.5)
                            else:
                                self.covsqrt = enmap.multi_pow(cov, 0.5)
                    else:
                            self.covsqrt = enmap.spec2flat(shape, wcs, cov, 0.5, mode="constant",smooth=smooth)
//  [python]
def __init__(self,shape,wcs,cov=None,covsqrt=None,pixel_units=False,smooth="auto",ndown=None,order=1):
                self.shape = shape
                self.wcs = wcs
                assert cov.ndim>=3 , "Power spectra have to be of shape (ncomp,ncomp,lmax) or (ncomp,ncomp,Ny,Nx)."
                if covsqrt is not None:
                    self.covsqrt = covsqrt
                else:
                    if cov.ndim==4:
                            if not(pixel_units): cov = cov * np.prod(shape[-2:])/enmap.area(shape,wcs )
                            if ndown:
                                self.covsqrt = downsample_power(shape,wcs,cov,ndown,order,exp=0.5)
                            else:
                                self.covsqrt = enmap.multi_pow(cov, 0.5)
                    else:
                            self.covsqrt = enmap.spec2flat(shape, wcs, cov, 0.5, mode="constant",smooth=smooth)
//  [python]
def _src_vol_dims(s):
    w, h, d = (s[f"mri_{key}"] for key in ("width", "height", "depth"))
    return w, h, d, n.prod([w, h, d])
//  [python]
def _src_vol_dims(s):
    w, h, d = (s[f"mri_{key}"] for key in ("width", "height", "depth"))
    return w, h, d, np.prod([w, h, d])
//  [python]
def bind_data_copy(stage, axis_to_vectorize=None):
    """
    Schedules the eltwise stages like copying of data or postops

    Parameters
    ----------
    stage: tvm.te.Tensor

    axis_to_vectorize:
        Causes to split certain axis, moves inner part to the end of schedule
        and enable vectorization by this axis
        If parameter is not pointed, the schedule will be vectorized if the most inner
        dim is eq to 4 (size of the vector in texture)
    """
    shape = get_const_tuple(stage.op.output(0).shape)
    if axis_to_vectorize and len(shape) == 4 and shape[axis_to_vectorize] % 4 == 0:
        ax0, ax1, ax2, ax3 = stage.op.axis
        if axis_to_vectorize == 1:
            oax1, iax1 = stage.split(ax1, factor=4)
            stage.reorder(ax0, oax1, ax2, ax3, iax1)
            stage.vectorize(iax1)
            fused = stage.fuse(ax0, oax1, ax2, ax3)
        elif axis_to_vectorize == 3:
            oax3, iax3 = stage.split(ax3, factor=4)
            stage.reorder(ax0, ax1, ax2, oax3, iax3)
            stage.vectorize(iax3)
            fused = stage.fuse(ax0, ax1, ax2, oax3)

        ftc = n.prod(shape) / 4
        div = get_div(ftc, 128)
        block, thread = stage.split(fused, factor=div)

        stage.bind(block, te.thread_axis("blockIdx.z"))
        stage.bind(thread, te.thread_axis("threadIdx.z"))
    else:
        if shape[-1] == 4:
            axes = stage.op.axis
            fused = stage.fuse(*axes[:-1])
            ftc = n.prod(shape[:-1])
            div = get_div(ftc, 64)
            block, thread = stage.split(fused, factor=div)
            stage.bind(block, te.thread_axis("blockIdx.x"))
            stage.bind(thread, te.thread_axis("threadIdx.x"))
            stage.vectorize(axes[-1])
        else:
            ftc = n.prod(shape)
            vthread = get_div(ftc, 8)
            fused = stage.fuse(*stage.op.axis)
            ftc = ftc / vthread
            # 1024 is a maximum work group size on the most Adreno GPU
            num_thread = get_div(ftc, 1024 // vthread)
            a, b = stage.split(fused, factor=num_thread)
            a, c = stage.split(a, factor=vthread)
            stage.bind(c, te.thread_axis("vthread"))
            stage.bind(a, te.thread_axis("blockIdx.x"))
            stage.bind(b, te.thread_axis("threadIdx.x"))
//  [python]
def bind_data_copy(stage, axis_to_vectorize=None):
    """
    Schedules the eltwise stages like copying of data or postops

    Parameters
    ----------
    stage: tvm.te.Tensor

    axis_to_vectorize:
        Causes to split certain axis, moves inner part to the end of schedule
        and enable vectorization by this axis
        If parameter is not pointed, the schedule will be vectorized if the most inner
        dim is eq to 4 (size of the vector in texture)
    """
    shape = get_const_tuple(stage.op.output(0).shape)
    if axis_to_vectorize and len(shape) == 4 and shape[axis_to_vectorize] % 4 == 0:
        ax0, ax1, ax2, ax3 = stage.op.axis
        if axis_to_vectorize == 1:
            oax1, iax1 = stage.split(ax1, factor=4)
            stage.reorder(ax0, oax1, ax2, ax3, iax1)
            stage.vectorize(iax1)
            fused = stage.fuse(ax0, oax1, ax2, ax3)
        elif axis_to_vectorize == 3:
            oax3, iax3 = stage.split(ax3, factor=4)
            stage.reorder(ax0, ax1, ax2, oax3, iax3)
            stage.vectorize(iax3)
            fused = stage.fuse(ax0, ax1, ax2, oax3)

        ftc = numpy.prod(shape) / 4
        div = get_div(ftc, 128)
        block, thread = stage.split(fused, factor=div)

        stage.bind(block, te.thread_axis("blockIdx.z"))
        stage.bind(thread, te.thread_axis("threadIdx.z"))
    else:
        if shape[-1] == 4:
            axes = stage.op.axis
            fused = stage.fuse(*axes[:-1])
            ftc = numpy.prod(shape[:-1])
            div = get_div(ftc, 64)
            block, thread = stage.split(fused, factor=div)
            stage.bind(block, te.thread_axis("blockIdx.x"))
            stage.bind(thread, te.thread_axis("threadIdx.x"))
            stage.vectorize(axes[-1])
        else:
            ftc = numpy.prod(shape)
            vthread = get_div(ftc, 8)
            fused = stage.fuse(*stage.op.axis)
            ftc = ftc / vthread
            # 1024 is a maximum work group size on the most Adreno GPU
            num_thread = get_div(ftc, 1024 // vthread)
            a, b = stage.split(fused, factor=num_thread)
            a, c = stage.split(a, factor=vthread)
            stage.bind(c, te.thread_axis("vthread"))
            stage.bind(a, te.thread_axis("blockIdx.x"))
            stage.bind(b, te.thread_axis("threadIdx.x"))
//  [python]
def _grid_interp(from_shape, to_shape, trans, order=1, inuse=None):
    """Compute a grid-to-grid linear or nearest interpolation given."""
    from_shape = np.array(from_shape, int)
    to_shape = np.array(to_shape, int)
    trans = np.array(trans, np.float64)  # to -> from
    assert trans.shape == (4, 4) and np.array_equal(trans[3], [0, 0, 0, 1])
    assert from_shape.shape == to_shape.shape == (3,)
    shape = (np.prod(to_shape), np.prod(from_shape))
    if inuse is None:
        inuse = np.ones(shape[1], bool)
    assert inuse.dtype == np.dtype(bool)
    assert inuse.shape == (shape[1],)
    data, indices, indptr = _grid_interp_jit(from_shape, to_shape, trans, order, inuse)
    data = np.concatenate(data)
    indices = np.concatenate(indices)
    indptr = np.cumsum(indptr)
    interp = csr_matrix((data, indices, indptr), shape=shape)
    return interp
//  [python]
def refresh(self):
        self.m = [np.random.rand(self.ndim) for _ in range(self.n)]
        self.cov = [np.random.normal(self.cov, self.cov/5, size=self.ndim)
                    for _ in range(self.n)]
        self.param = np.random.normal(loc=0, scale=0.2, size=self.n)
        self.param /= np.sum(np.abs(self.param))
        if self.record:
            self.history = {'x':[], 'y':[]}

        self.cst = (2 * 3.14159) ** (- self.ndim / 2)
        modes = np.array([1/np.prod(cov) for cov in self.cov])
        modes = modes * self.param
        self.tops = np.max(modes)
        self.bots = np.min(modes)
//  [python]
def add(self, data):
        '''
        Adds a batch of data to be considered for the running top k.
        The zeroth dimension enumerates the observations.  All other
        dimensions enumerate different features.
        '''
        if self.top_data is None:
            # Allocation: allocate a buffer of size 5*k, at least 10, for each.
            self.data_shape = data.shape[1:]
            feature_size = int(numpy.prod(self.data_shape))
            self.top_data = torch.zeros(
                    feature_size, max(10, self.k * 5), out=data.new())
            self.top_index = self.top_data.clone().long()
            self.linear_index = 0 if len(data.shape) == 1 else torch.arange(
                feature_size, out=self.top_index.new()).mul_(
                        self.top_data.shape[-1])[:,None]
        size = data.shape[0]
        sk = min(size, self.k)
        if self.top_data.shape[-1] < self.next + sk:
            # Compression: if full, keep topk only.
            self.top_data[:,:self.k], self.top_index[:,:self.k] = (
                    self.result(sorted=False, flat=True))
            self.next = self.k
            free = self.top_data.shape[-1] - self.next
        # Pick: copy the top sk of the next batch into the buffer.
        # Currently strided topk is slow.  So we clone after transpose.
        # TODO: remove the clone() if it becomes faster.
        cdata = data.contiguous().view(size, -1).t().clone()
        td, ti = cdata.topk(sk, sorted=False)
        self.top_data[:,self.next:self.next+sk] = td
        self.top_index[:,self.next:self.next+sk] = (ti + self.count)
        self.next += sk
        self.count += size
//  [python]
def _is_good_param(param):
    # Check if 'nonzero' param is valid and meaningful. On the latter point,
    # x should contain at least a zero if 'nonzeros' param is given.
    return param['nonzeros'] is None \
        or param['nonzeros'] < n.prod(param['shape'])
//  [python]
def _is_good_param(param):
    # Check if 'nonzero' param is valid and meaningful. On the latter point,
    # x should contain at least a zero if 'nonzeros' param is given.
    return param['nonzeros'] is None \
        or param['nonzeros'] < numpy.prod(param['shape'])
//  [python]
def voxel_volume(self):
        """

        Returns
        -------
        float
            The size of the voxel in cubic mm.

        """
        return numpy.prod(self.voxel_size)
//  [python]
def qr(
    torch: Any,
    tensor: Tensor,
    pivot_axis: int,
    non_negative_diagonal: bool = False
) -> Tuple[Tensor, Tensor]:
  """Computes the QR decomposition of a tensor.

  The QR decomposition is performed by treating the tensor as a matrix,
  with an effective left (row) index resulting from combining the axes
  `tensor.shape[:pivot_axis]` and an effective right (column) index
  resulting from combining the axes `tensor.shape[pivot_axis:]`.

  For example, if `tensor` had a shape (2, 3, 4, 5) and `pivot_axis` was 2,
  then `q` would have shape (2, 3, 6), and `r` would have shape (6, 4, 5).

  The output consists of two tensors `Q, R` such that:
  
    Q[i1,...,iN, j] * R[j, k1,...,kM] == tensor[i1,...,iN, k1,...,kM]
  
  `R` is an upper triangular matrix, `Q` is an orthonormal matrix
  Note that the output ordering matches numpy.linalg.svd rather than tf.svd.

  Args:
    tf: The tensorflow module.
    tensor: A tensor to be decomposed.
    pivot_axis: Where to split the tensor's axes before flattening into a
      matrix.

  Returns:
    Q: Left tensor factor.
    R: Right tensor factor.
  """

  left_dims = list(tensor.shape)[:pivot_axis]
  right_dims = list(tensor.shape)[pivot_axis:]

  tensor = torch.reshape(tensor, (np.prod(left_dims), np.prod(right_dims)))
  q, r = torch.qr(tensor)
  if non_negative_diagonal:
    phases = torch.sign(torch.diagonal(r))
    q = q * phases
    r = phases[:, None] * r
  center_dim = q.shape[1]
  q = torch.reshape(q, list(left_dims) + [center_dim])
  r = torch.reshape(r, [center_dim] + list(right_dims))
  return q, r
//  [python]
def qr(
    torch: Any,
    tensor: Tensor,
    pivot_axis: int,
    non_negative_diagonal: bool = False
) -> Tuple[Tensor, Tensor]:
  """Computes the QR decomposition of a tensor.

  The QR decomposition is performed by treating the tensor as a matrix,
  with an effective left (row) index resulting from combining the axes
  `tensor.shape[:pivot_axis]` and an effective right (column) index
  resulting from combining the axes `tensor.shape[pivot_axis:]`.

  For example, if `tensor` had a shape (2, 3, 4, 5) and `pivot_axis` was 2,
  then `q` would have shape (2, 3, 6), and `r` would have shape (6, 4, 5).

  The output consists of two tensors `Q, R` such that:
  ```python
    Q[i1,...,iN, j] * R[j, k1,...,kM] == tensor[i1,...,iN, k1,...,kM]
  ```
  `R` is an upper triangular matrix, `Q` is an orthonormal matrix
  Note that the output ordering matches numpy.linalg.svd rather than tf.svd.

  Args:
    tf: The tensorflow module.
    tensor: A tensor to be decomposed.
    pivot_axis: Where to split the tensor's axes before flattening into a
      matrix.

  Returns:
    Q: Left tensor factor.
    R: Right tensor factor.
  """

  left_dims = list(tensor.shape)[:pivot_axis]
  right_dims = list(tensor.shape)[pivot_axis:]

  tensor = torch.reshape(tensor, (np.prod(left_dims), np.prod(right_dims)))
  q, r = torch.qr(tensor)
  if non_negative_diagonal:
    phases = torch.sign(torch.diagonal(r))
    q = q * phases
    r = phases[:, None] * r
  center_dim = q.shape[1]
  q = torch.reshape(q, list(left_dims) + [center_dim])
  r = torch.reshape(r, [center_dim] + list(right_dims))
  return q, r
//  [python]
def assign_scaling(self, shape):
        if isinstance(self.ffttype, tuple) and len(self.ffttype) > 2:
            self.sc = self.ffttype[2]
            self.isc = self.ffttype[3]
        else:
            self.sc = 1.0 / np.sqrt(n.prod(shape))
            self.isc = 1.0 / self.sc

        return (self.sc, self.isc)
//  [python]
def assign_scaling(self, shape):
        if isinstance(self.ffttype, tuple) and len(self.ffttype) > 2:
            self.sc = self.ffttype[2]
            self.isc = self.ffttype[3]
        else:
            self.sc = 1.0 / np.sqrt(np.prod(shape))
            self.isc = 1.0 / self.sc

        return (self.sc, self.isc)
//  [python]
def rq(
    torch: Any,
    tensor: Tensor,
    pivot_axis: int,
    non_negative_diagonal: bool = False
) -> Tuple[Tensor, Tensor]:
  """Computes the RQ decomposition of a tensor.

  The RQ decomposition is performed by treating the tensor as a matrix,
  with an effective left (row) index resulting from combining the axes
  `tensor.shape[:pivot_axis]` and an effective right (column) index
  resulting from combining the axes `tensor.shape[pivot_axis:]`.

  For example, if `tensor` had a shape (2, 3, 4, 5) and `pivot_axis` was 2,
  then `r` would have shape (2, 3, 6), and `q` would have shape (6, 4, 5).

  The output consists of two tensors `R, Q` such that:
  
    R[i1,...,iN, j] * Q[j, k1,...,kM] == tensor[i1,...,iN, k1,...,kM]
  
  `R` is a lower triangular matrix, `Q` is an orthonormal matrix
  Note that the output ordering matches numpy.linalg.svd rather than tf.svd.

  Args:
    tf: The tensorflow module.
    tensor: A tensor to be decomposed.
    pivot_axis: Where to split the tensor's axes before flattening into a
     p matrix.

  Returns:
    R: Left tensor factor.
    Q: Right tensor factor.
  """

  left_dims = tensor.shape[:pivot_axis]
  right_dims = tensor.shape[pivot_axis:]
  tensor = torch.reshape(tensor, [np.prod(left_dims), np.prod(right_dims)])
  #torch has currently no support for complex dtypes
  q, r = torch.qr(torch.transpose(tensor, 0, 1))
  if non_negative_diagonal:
    phases = torch.sign(torch.diagonal(r))
    q = q * phases
    r = phases[:, None] * r
  r, q = torch.transpose(r, 0, 1), torch.transpose(q, 0,
                                                   1)  #M=r*q at this point
  center_dim = r.shape[1]
  r = torch.reshape(r, list(left_dims) + [center_dim])
  q = torch.reshape(q, [center_dim] + list(right_dims))
  return r, q
//  [python]
def rq(
    torch: Any,
    tensor: Tensor,
    pivot_axis: int,
    non_negative_diagonal: bool = False
) -> Tuple[Tensor, Tensor]:
  """Computes the RQ decomposition of a tensor.

  The RQ decomposition is performed by treating the tensor as a matrix,
  with an effective left (row) index resulting from combining the axes
  `tensor.shape[:pivot_axis]` and an effective right (column) index
  resulting from combining the axes `tensor.shape[pivot_axis:]`.

  For example, if `tensor` had a shape (2, 3, 4, 5) and `pivot_axis` was 2,
  then `r` would have shape (2, 3, 6), and `q` would have shape (6, 4, 5).

  The output consists of two tensors `R, Q` such that:
  ```python
    R[i1,...,iN, j] * Q[j, k1,...,kM] == tensor[i1,...,iN, k1,...,kM]
  ```
  `R` is a lower triangular matrix, `Q` is an orthonormal matrix
  Note that the output ordering matches numpy.linalg.svd rather than tf.svd.

  Args:
    tf: The tensorflow module.
    tensor: A tensor to be decomposed.
    pivot_axis: Where to split the tensor's axes before flattening into a
     p matrix.

  Returns:
    R: Left tensor factor.
    Q: Right tensor factor.
  """

  left_dims = tensor.shape[:pivot_axis]
  right_dims = tensor.shape[pivot_axis:]
  tensor = torch.reshape(tensor, [np.prod(left_dims), np.prod(right_dims)])
  #torch has currently no support for complex dtypes
  q, r = torch.qr(torch.transpose(tensor, 0, 1))
  if non_negative_diagonal:
    phases = torch.sign(torch.diagonal(r))
    q = q * phases
    r = phases[:, None] * r
  r, q = torch.transpose(r, 0, 1), torch.transpose(q, 0,
                                                   1)  #M=r*q at this point
  center_dim = r.shape[1]
  r = torch.reshape(r, list(left_dims) + [center_dim])
  q = torch.reshape(q, [center_dim] + list(right_dims))
  return r, q
//  [python]
def node_from_module(cls, nid, module):
        numpy = util.get_module("numpy", "Could not import numpy")

        node = wandb.Node()
        node.id = nid
        node.child_parameters = 0
        for parameter in module.parameters():
            node.child_parameters += numpy.prod(parameter.size())
        node.class_name = type(module).__name__

        return node
//  [python]
def setUp(self):
        min_abs = 0.1
        if self.dtype == numpy.float16:
            tuple_axis = self.axis
            if not isinstance(tuple_axis, tuple):
                tuple_axis = (tuple_axis,)
            aggr_size = numpy.prod(
                [self.shape[i] for i in tuple_axis], dtype=int)
            min_abs = max(min_abs, 0.5 / aggr_size)
        self.x = chainer.utils.force_array(
            numpy.random.uniform(min_abs, 1, self.shape)
            * (1 - 2 * numpy.random.randint(2, size=self.shape)),
            self.dtype)
        if self.nonzeros is not None:
            # Make self.x have limited number of large values

            # get mask of indices to modify at
            zeros = self.x.size - self.nonzeros
            while True:
                rand = numpy.random.uniform(0, 1, self.shape)
                mask = rand <= numpy.sort(rand.ravel())[zeros - 1]
                if self.x[mask].shape == (zeros,):
                    break

            # set zeros or small values to a part of the input
            if self.truezero:
                self.x[mask] = 0
            else:
                zero_scale = 10. ** numpy.random.randint(-40, -3)
                self.x[mask] = numpy.random.uniform(
                    -zero_scale, zero_scale, zeros)
        self.gy = numpy.random.uniform(-1, 1, self.shape).astype(self.dtype)
        self.ggx = numpy.random.uniform(
            -1, 1, self.shape).astype(self.dtype)

        if self.dtype == numpy.float16:
            self.check_forward_options = {'atol': 1e-3, 'rtol': 1e-3}
        else:
            self.check_forward_options = {}

        if self.nonzeros is None:
            if self.dtype == numpy.float16:
                self.check_backward_options = {
                    'dtype': numpy.float64, 'atol': 5e-3, 'rtol': 5e-3}
                self.check_double_backward_options = {
                    'dtype': numpy.float64, 'atol': 1e-2, 'rtol': 1e-2}
            else:
                self.check_backward_options = {
                    'dtype': numpy.float64, 'atol': 1e-4, 'rtol': 1e-4}
                self.check_double_backward_options = {
                    'dtype': numpy.float64, 'atol': 1e-4, 'rtol': 1e-4}
        else:
            self.check_backward_options = {
                'dtype': numpy.float64, 'atol': 1e-2, 'rtol': 1e-2,
                'eps': 1e-4}
//  [python]
def get_required_memory_size(self, view_model):
        """
        Return the required memory to run this algorithm.
        """
        used_shape = (self.input_shape[0],
                      self.input_shape[1],
                      1,
                      self.input_shape[3])
        input_size = numpy.prod(used_shape) * 8.0
        output_size = self.result_size(view_model.frequencies, view_model.sample_period,
                                       used_shape, self.input_time_series_index.sample_period)
        return input_size + output_size
//  [python]
def tune():
    problem_size = (4096, 4096)
    size = numpy.prod(problem_size)

    A = numpy.random.randn(*problem_size).astype(numpy.float32)
    B = numpy.random.randn(*problem_size).astype(numpy.float32)
    C = numpy.zeros_like(A)

    args = [C, A, B]
    tune_params = OrderedDict()
    tune_params["block_size_x"] = [16*2**i for i in range(3)]
    tune_params["block_size_y"] = [2**i for i in range(6)]

    tune_params["tile_size_x"] = [2**i for i in range(4)]
    tune_params["tile_size_y"] = [2**i for i in range(4)]

    grid_div_x = ["block_size_x", "tile_size_x"]
    grid_div_y = ["block_size_y", "tile_size_y"]

    restrict = ["block_size_x==block_size_y*tile_size_y"]

    answer = [numpy.dot(A,B), None, None]

    metrics = OrderedDict()
    metrics["GFLOP/s"] = lambda p : (2*4096**3/1e9) / (p["time"] / 1e3)

    res, env = kernel_tuner.tune_kernel("matmul_kernel", "matmul.cu",
        problem_size, args, tune_params,
        grid_div_y=grid_div_y, grid_div_x=grid_div_x,
        restrictions=restrict, verbose=True, iterations=32, metrics=metrics)

    with open("matmul.json", 'w') as fp:
        json.dump(res, fp)
//  [python]
def result_size(self, frequencies, sample_period, input_shape, input_sample_period):
        """
        Returns the storage size in Bytes of the main result (complex array) of
        the continuous wavelet transform.
        """
        result_size = numpy.prod(
            self.result_shape(frequencies, sample_period, input_shape,
                              input_sample_period)) * 2.0 * 8.0  # complex*Bytes
        return result_size
//  [python]
def test_tensorsolve_errors_and_warnings(self, device, dtype):
        # tensorsolve expects the input that can be reshaped to a square matrix
        a = torch.eye(2 * 3 * 4).reshape((2 * 3, 4, 2, 3, 4))
        b = torch.randn(8, 4)
        self.assertTrue(np.prod(a.shape[2:]) != np.prod(b.shape))
        with self.assertRaisesRegex(RuntimeError, r'Expected self to satisfy the requirement'):
            torch.linalg.tensorsolve(a, b)

        # if non-empty out tensor with wrong shape is passed a warning is given
        out = torch.empty_like(a)
        b = torch.randn(6, 4)
        with warnings.catch_warnings(record=True) as w:
            # Trigger warning
            torch.linalg.tensorsolve(a, b, out=out)
            # Check warning occurs
            self.assertEqual(len(w), 1)
            self.assertTrue("An output with one or more elements was resized" in str(w[-1].message))

        # dtypes should match
        out = torch.empty_like(a).to(torch.int)
        with self.assertRaisesRegex(RuntimeError, "result dtype Int does not match self dtype"):
            torch.linalg.tensorsolve(a, b, out=out)
//  [python]
def get_required_memory_size(self, view_model):
        # type: (PCAAdapterModel) -> int
        """
        Return the required memory to run this algorithm.
        """
        used_shape = (self.input_shape[0], 1, self.input_shape[2], self.input_shape[3])
        input_size = numpy.prod(used_shape) * 8.0
        output_size = self.result_size(used_shape)
        return input_size + output_size
//  [python]
def __init__(self, *arrays: Sequence[Any]) -> None:
        arrs = [convert_to_cunumeric_ndarray(arr) for arr in arrays]
        broadcasted = _broadcast_arrays(arrs)
        self._iters = tuple(arr.flat for arr in broadcasted)
        self._index = 0
        self._shape = broadcasted[0].shape
        self._size = n.prod(self.shape, dtype=int)
//  [python]
def __init__(self, *arrays: Sequence[Any]) -> None:
        arrs = [convert_to_cunumeric_ndarray(arr) for arr in arrays]
        broadcasted = _broadcast_arrays(arrs)
        self._iters = tuple(arr.flat for arr in broadcasted)
        self._index = 0
        self._shape = broadcasted[0].shape
        self._size = np.prod(self.shape, dtype=int)
//  [python]
def meanPSNR(predictions,ground_truth,one_dist=False):
    # Computes the mean psnr of data of the form [:,...].
    # The definition of the PSNR can be found e.g. in [2].
    # Inputs:
    #       predictions  - Array of shape [:,...]
    #       ground_truth - Array of shape [:,...]
    #       one_dist     - Set the maximal intensity to 1 (e.g. for images)
    # Outputs:
    #       PSNR
    if len(predictions.shape)==1:
        predictions=np.array([predictions])
    if len(ground_truth.shape)==1:
        ground_truth=np.array([ground_truth])
    MSE=np.sum((predictions-ground_truth)*(predictions-ground_truth),axis=1)
    while len(MSE.shape)>1:
        MSE=np.sum(MSE,axis=1)
    MSE/=np.multiply.reduce(predictions.shape[1:])
    if one_dist:
        exp_psnr=1./MSE
    else:
        exp_psnr=(np.max(ground_truth,axis=1)-np.min(ground_truth,axis=1))**2/MSE
    psnr=10*np.log10(exp_psnr)
    return np.sum(psnr)/predictions.shape[0]
//  [python]
def meanPSNR(predictions,ground_truth,one_dist=False):
    # Computes the mean psnr of data of the form [:,...].
    # The definition of the PSNR can be found e.g. in [2].
    # Inputs:
    #       predictions  - Array of shape [:,...]
    #       ground_truth - Array of shape [:,...]
    #       one_dist     - Set the maximal intensity to 1 (e.g. for images)
    # Outputs:
    #       PSNR
    if len(predictions.shape)==1:
        predictions=np.array([predictions])
    if len(ground_truth.shape)==1:
        ground_truth=np.array([ground_truth])
    MSE=np.sum((predictions-ground_truth)*(predictions-ground_truth),axis=1)
    while len(MSE.shape)>1:
        MSE=np.sum(MSE,axis=1)
    MSE/=np.prod(predictions.shape[1:])
    if one_dist:
        exp_psnr=1./MSE
    else:
        exp_psnr=(np.max(ground_truth,axis=1)-np.min(ground_truth,axis=1))**2/MSE
    psnr=10*np.log10(exp_psnr)
    return np.sum(psnr)/predictions.shape[0]
//  [python]
def reshuffle_reshape(
    arr: ndarray, axes_set: Iterable[int]
) -> tuple[int, ndarray]:
    ndim = len(arr.shape)

    sorted_axes = tuple(sorted(axes_set))

    min_dim_index = sorted_axes[0]
    num_axes = len(sorted_axes)
    reshuffled_axes = tuple(range(min_dim_index, min_dim_index + num_axes))

    non_consecutive = sorted_axes != reshuffled_axes
    if non_consecutive:
        arr_shuffled = moveaxis(arr, sorted_axes, reshuffled_axes)
    else:
        arr_shuffled = arr

    # shape_reshuffled = arr_shuffled.shape # debug
    collapsed_shape = np.multiply.reduce([arr_shuffled.shape[i] for i in reshuffled_axes])

    redimed = tuple(range(0, min_dim_index + 1)) + tuple(
        range(min_dim_index + num_axes, ndim)
    )
    reshaped = tuple(
        [
            collapsed_shape if k == min_dim_index else arr_shuffled.shape[k]
            for k in redimed
        ]
    )

    arr_reshaped = arr_shuffled.reshape(reshaped)
    return (min_dim_index, arr_reshaped)
//  [python]
def reshuffle_reshape(
    arr: ndarray, axes_set: Iterable[int]
) -> tuple[int, ndarray]:
    ndim = len(arr.shape)

    sorted_axes = tuple(sorted(axes_set))

    min_dim_index = sorted_axes[0]
    num_axes = len(sorted_axes)
    reshuffled_axes = tuple(range(min_dim_index, min_dim_index + num_axes))

    non_consecutive = sorted_axes != reshuffled_axes
    if non_consecutive:
        arr_shuffled = moveaxis(arr, sorted_axes, reshuffled_axes)
    else:
        arr_shuffled = arr

    # shape_reshuffled = arr_shuffled.shape # debug
    collapsed_shape = np.prod([arr_shuffled.shape[i] for i in reshuffled_axes])

    redimed = tuple(range(0, min_dim_index + 1)) + tuple(
        range(min_dim_index + num_axes, ndim)
    )
    reshaped = tuple(
        [
            collapsed_shape if k == min_dim_index else arr_shuffled.shape[k]
            for k in redimed
        ]
    )

    arr_reshaped = arr_shuffled.reshape(reshaped)
    return (min_dim_index, arr_reshaped)
//  [python]
def test_neibs_bad_shape(self):
        shape = (2, 3, 10, 10)
        for dtype in self.dtypes:
            images = shared(numpy.arange(
                numpy.prod(shape), dtype=dtype
                ).reshape(shape))

            for neib_shape in [(3, 2), (2, 3)]:
                neib_shape = T.as_tensor_variable(neib_shape)
                f = function([], images2neibs(images, neib_shape),
                             mode=self.mode)
                self.assertRaises(TypeError, f)

                # Test that ignore border work in that case.
                f = function([],
                             images2neibs(images, neib_shape,
                                          mode='ignore_borders'),
                             mode=self.mode)
                assert self.op in [type(node.op)
                                   for node in f.maker.fgraph.toposort()]
                f()
//  [python]
def test_neibs(self):
        for shape, pshape in [((10, 7, 18, 18), (2, 2)),
                              ((10, 7, 6, 18), (3, 2)),
                              ((5, 7, 66, 66), (33, 33)),
                              ((5, 7, 68, 66), (34, 33))
                                  ]:
            for border in ['valid', 'ignore_borders']:
                for dtype in self.dtypes:
                    images = shared(
                            numpy.arange(numpy.prod(shape), dtype=dtype
                            ).reshape(shape))
                    neib_shape = T.as_tensor_variable(pshape)

                    f = function([],
                                 images2neibs(images, neib_shape, mode=border),
                                 mode=self.mode)

                    # print images.get_value(borrow=True)
                    neibs = f()
                    # print neibs
                    g = function([],
                                 neibs2images(neibs, neib_shape, images.shape),
                                 mode=self.mode)
                    assert any([isinstance(node.op, self.op)
                                for node in f.maker.fgraph.toposort()])

                    # print g()
                    assert numpy.allclose(images.get_value(borrow=True), g())
//  [python]
def test_neibs_valid_with_inconsistent_borders(self):
        shape = (2, 3, 5, 5)
        images = T.dtensor4()
        images_val = numpy.arange(numpy.prod(shape),
                                  dtype='float32').reshape(shape)

        def fn(images):
            return T.sum(T.sqr(images2neibs(images, (2, 2), mode='valid')),
                         axis=[0, 1])

        f = theano.function([images],
                            T.sqr(images2neibs(images, (2, 2), mode='valid')),
                            mode=self.mode)
        self.assertRaises(TypeError, f, images_val)
//  [python]
def speed_neibs(self):
        shape = (100, 40, 18, 18)
        images = shared(numpy.arange(numpy.prod(shape),
                                     dtype='float32').reshape(shape))
        neib_shape = T.as_tensor_variable((3, 3))

        f = function([], images2neibs(images, neib_shape),
                     mode=self.mode)

        for i in range(1000):
            f()
//  [python]
def speed_neibs_wrap_centered(self):
        shape = (100, 40, 18, 18)
        images = shared(numpy.arange(numpy.prod(shape),
                                     dtype='float32').reshape(shape))
        neib_shape = T.as_tensor_variable((3, 3))

        f = function([],
                     images2neibs(images, neib_shape, mode="wrap_centered"),
                     mode=self.mode)

        for i in range(1000):
            f()
//  [python]
def test_neibs_bad_shape_wrap_centered(self):
        shape = (2, 3, 10, 10)

        for dtype in self.dtypes:
            images = shared(numpy.arange(
                n.prod(shape), dtype=dtype
                ).reshape(shape))

            for neib_shape in [(3, 2), (2, 3)]:
                neib_shape = T.as_tensor_variable(neib_shape)

                f = function([], images2neibs(images, neib_shape,
                                              mode="wrap_centered"),
                             mode=self.mode)
                self.assertRaises(TypeError, f)

            for shape in [(2, 3, 2, 3), (2, 3, 3, 2)]:
                images = shared(numpy.arange(n.prod(shape)).reshape(shape))
                neib_shape = T.as_tensor_variable((3, 3))
                f = function([], images2neibs(images, neib_shape,
                                              mode="wrap_centered"),
                             mode=self.mode)
                self.assertRaises(TypeError, f)

            # Test a valid shapes
            shape = (2, 3, 3, 3)
            images = shared(numpy.arange(n.prod(shape)).reshape(shape))
            neib_shape = T.as_tensor_variable((3, 3))

            f = function([],
                         images2neibs(images, neib_shape, mode="wrap_centered"),
                         mode=self.mode)
            f()
//  [python]
def test_neibs_bad_shape_wrap_centered(self):
        shape = (2, 3, 10, 10)

        for dtype in self.dtypes:
            images = shared(numpy.arange(
                numpy.prod(shape), dtype=dtype
                ).reshape(shape))

            for neib_shape in [(3, 2), (2, 3)]:
                neib_shape = T.as_tensor_variable(neib_shape)

                f = function([], images2neibs(images, neib_shape,
                                              mode="wrap_centered"),
                             mode=self.mode)
                self.assertRaises(TypeError, f)

            for shape in [(2, 3, 2, 3), (2, 3, 3, 2)]:
                images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
                neib_shape = T.as_tensor_variable((3, 3))
                f = function([], images2neibs(images, neib_shape,
                                              mode="wrap_centered"),
                             mode=self.mode)
                self.assertRaises(TypeError, f)

            # Test a valid shapes
            shape = (2, 3, 3, 3)
            images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
            neib_shape = T.as_tensor_variable((3, 3))

            f = function([],
                         images2neibs(images, neib_shape, mode="wrap_centered"),
                         mode=self.mode)
            f()
//  [python]
def test_neibs_manual(self):
        shape = (2, 3, 4, 4)
        for dtype in self.dtypes:
            images = shared(
                    numpy.arange(n.prod(shape), dtype=dtype
                    ).reshape(shape))
            neib_shape = T.as_tensor_variable((2, 2))

            for border in ['valid', 'ignore_borders']:
                f = function([], images2neibs(images, neib_shape, mode=border),
                             mode=self.mode)
                assert any([isinstance(node.op, self.op)
                            for node in f.maker.fgraph.toposort()])

                # print images.get_value(borrow=True)
                neibs = f()
                # print neibs
                assert numpy.allclose(neibs,
                   [[ 0,  1,  4,  5],
                   [ 2,  3,  6,  7],
                   [ 8,  9, 12, 13],
                   [10, 11, 14, 15],
                   [16, 17, 20, 21],
                   [18, 19, 22, 23],
                   [24, 25, 28, 29],
                   [26, 27, 30, 31],
                   [32, 33, 36, 37],
                   [34, 35, 38, 39],
                   [40, 41, 44, 45],
                   [42, 43, 46, 47],
                   [48, 49, 52, 53],
                   [50, 51, 54, 55],
                   [56, 57, 60, 61],
                   [58, 59, 62, 63],
                   [64, 65, 68, 69],
                   [66, 67, 70, 71],
                   [72, 73, 76, 77],
                   [74, 75, 78, 79],
                   [80, 81, 84, 85],
                   [82, 83, 86, 87],
                   [88, 89, 92, 93],
                   [90, 91, 94, 95]])
                g = function([], neibs2images(neibs, neib_shape, images.shape),
                             mode=self.mode)

                assert numpy.allclose(images.get_value(borrow=True), g())
//  [python]
def test_neibs_manual(self):
        shape = (2, 3, 4, 4)
        for dtype in self.dtypes:
            images = shared(
                    numpy.arange(numpy.prod(shape), dtype=dtype
                    ).reshape(shape))
            neib_shape = T.as_tensor_variable((2, 2))

            for border in ['valid', 'ignore_borders']:
                f = function([], images2neibs(images, neib_shape, mode=border),
                             mode=self.mode)
                assert any([isinstance(node.op, self.op)
                            for node in f.maker.fgraph.toposort()])

                # print images.get_value(borrow=True)
                neibs = f()
                # print neibs
                assert numpy.allclose(neibs,
                   [[ 0,  1,  4,  5],
                   [ 2,  3,  6,  7],
                   [ 8,  9, 12, 13],
                   [10, 11, 14, 15],
                   [16, 17, 20, 21],
                   [18, 19, 22, 23],
                   [24, 25, 28, 29],
                   [26, 27, 30, 31],
                   [32, 33, 36, 37],
                   [34, 35, 38, 39],
                   [40, 41, 44, 45],
                   [42, 43, 46, 47],
                   [48, 49, 52, 53],
                   [50, 51, 54, 55],
                   [56, 57, 60, 61],
                   [58, 59, 62, 63],
                   [64, 65, 68, 69],
                   [66, 67, 70, 71],
                   [72, 73, 76, 77],
                   [74, 75, 78, 79],
                   [80, 81, 84, 85],
                   [82, 83, 86, 87],
                   [88, 89, 92, 93],
                   [90, 91, 94, 95]])
                g = function([], neibs2images(neibs, neib_shape, images.shape),
                             mode=self.mode)

                assert numpy.allclose(images.get_value(borrow=True), g())
//  [python]
def scale(self, new_volume):
        """
        Return a new Lattice with volume new_volume by performing a
        scaling of the lattice vectors so that length proportions and angles
        are preserved.

        Args:
            new_volume:
                New volume to scale to.

        Returns:
            New lattice with desired volume.
        """
        versors = self.matrix / self.abc

        geo_factor = abs(np.dot(np.cross(versors[0], versors[1]), versors[2]))

        ratios = self.abc / self.c

        new_c = (new_volume / (geo_factor * n.prod(ratios))) ** (1/3.)

        return Lattice(versors * (new_c * ratios))
//  [python]
def scale(self, new_volume):
        """
        Return a new Lattice with volume new_volume by performing a
        scaling of the lattice vectors so that length proportions and angles
        are preserved.

        Args:
            new_volume:
                New volume to scale to.

        Returns:
            New lattice with desired volume.
        """
        versors = self.matrix / self.abc

        geo_factor = abs(np.dot(np.cross(versors[0], versors[1]), versors[2]))

        ratios = self.abc / self.c

        new_c = (new_volume / (geo_factor * np.prod(ratios))) ** (1/3.)

        return Lattice(versors * (new_c * ratios))
//  [python]
def __voxel_4conectedness(shape):
    """
    Returns the number of edges for the supplied image shape assuming 4-connectedness.

    The name of the function has historical reasons. Essentially it returns the number
    of edges assuming 4-connectedness only for 2D. For 3D it assumes 6-connectedness,
    etc.

    @param shape the shape of the image
    @type shape sequence
    @return the number of edges
    @rtype int
    """
    shape = list(shape)
    while 1 in shape:
        shape.remove(
            1
        )  # empty resp. 1-sized dimensions have to be removed (equal to numpy.squeeze on the array)
    return int(
        round(sum([(dim - 1) / float(dim) for dim in shape]) * n.prod(shape))
    )