jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def q_impl(
        self,
        *q_inputs: ONNXOpInputOutputType,
        **attrs,
    ) -> ONNXOpInputOutputType:
        """Flatten the input integer encrypted tensor.

        Args:
            q_inputs: an encrypted integer tensor at index 0
            attrs: contains axis attribute

        Returns:
            result (QuantizedArray): reshaped encrypted integer tensor
        """

        prepared_inputs = self._prepare_inputs_with_constants(
            *q_inputs, calibrate=False, quantize_actual_values=True
        )

        # This op takes only encrypted inputs in the form of QuantizedArray
        assert isinstance(q_inputs[0], QuantizedArray)

        assert_true(len(q_inputs) == 1, "Flatten operator only takes a single input")

        axis = attrs["axis"]
        newshape: Sequence[SupportsIndex]
        newshape = (
            *q_inputs[0].qvalues.shape[0:axis],
            numpy.prod(q_inputs[0].qvalues.shape[axis:]),
        )

        # Return a new quantized array with the same quantization parameters
        return QuantizedArray(
            prepared_inputs[0].quantizer.n_bits,
            numpy.reshape(prepared_inputs[0].qvalues, newshape),
            value_is_float=False,
            options=prepared_inputs[0].quantizer.quant_options,
            stats=prepared_inputs[0].quantizer.quant_stats,
            params=prepared_inputs[0].quantizer.quant_params,
        )
//  [python]
def test_matmul(device, shape_pair):
    shape1, shape2 = shape_pair
    a1 = numpy.arange(numpy.prod(shape1)).reshape(shape1)
    a2 = numpy.arange(numpy.prod(shape2)).reshape(shape2)

    b1 = dpnp.asarray(a1, device=device)
    b2 = dpnp.asarray(a2, device=device)

    result = dpnp.matmul(b1, b2)
    expected = numpy.matmul(a1, a2)
    assert_allclose(expected, result)

    result_queue = result.sycl_queue
    assert_sycl_queue_equal(result_queue, b1.sycl_queue)
    assert_sycl_queue_equal(result_queue, b2.sycl_queue)
//  [python]
def test_inv(shape, is_empty, device):
    if is_empty:
        numpy_x = numpy.empty(shape, dtype=dpnp.default_float_type(device))
    else:
        count_elem = numpy.prod(shape)
        numpy_x = numpy.arange(
            1, count_elem + 1, dtype=dpnp.default_float_type()
        ).reshape(shape)

    dpnp_x = dpnp.array(numpy_x, device=device)

    result = dpnp.linalg.inv(dpnp_x)
    expected = numpy.linalg.inv(numpy_x)
    assert_dtype_allclose(result, expected)

    expected_queue = dpnp_x.get_array().sycl_queue
    result_queue = result.get_array().sycl_queue

    assert_sycl_queue_equal(result_queue, expected_queue)
//  [python]
def test_single_slice_requests_first_slices(graph, slice1, computeIn2d):
    dummy_filter_op = OpPassthroughFilter(graph=graph)
    shape = tuple(s.stop for s in ref_slicing)
    data_ref = numpy.arange(numpy.prod(shape)).astype(numpy.float32).reshape(shape)
    data = vigra.taggedView(data_ref.copy(), "tczyx")
    dummy_filter_op.Input.setValue(data)
    dummy_filter_op.ComputeIn2d.setValue(computeIn2d)
    req_roi = SubRegion(dummy_filter_op.Output, *sliceToRoi(slice1, data_ref.shape))
    target0 = numpy.zeros([x.stop - x.start for x in slice1], dtype=numpy.float32)

    dummy_filter_op.call_execute(
        slot=dummy_filter_op.Output,
        subindex=(),
        roi=req_roi,
        result=target0,
        sourceArray=data,
    )

    numpy.testing.assert_array_almost_equal(data_ref[slice1], target0)
//  [python]
def prod(self, a, axis=0):
        return n.prod(a, axis=axis)
//  [python]
def prod(self, a, axis=0):
        return np.prod(a, axis=axis)
//  [python]
def generate_inputs(self):
        x_size = numpy.multiply.reduce(self.x_shape)
        x = numpy.random.permutation(numpy.arange(x_size))\
            .reshape(self.x_shape).astype(self.dtype)
        return x,
//  [python]
def generate_inputs(self):
        x_size = numpy.prod(self.x_shape)
        x = numpy.random.permutation(numpy.arange(x_size))\
            .reshape(self.x_shape).astype(self.dtype)
        return x,
//  [python]
def test_qr(shape, mode, device):
    dtype = dpnp.default_float_type(device)
    count_elems = n.prod(shape)
    a = dpnp.arange(count_elems, dtype=dtype, device=device).reshape(shape)

    expected_queue = a.get_array().sycl_queue

    if mode == "r":
        dp_r = dpnp.linalg.qr(a, mode=mode)
        dp_r_queue = dp_r.get_array().sycl_queue
        assert_sycl_queue_equal(dp_r_queue, expected_queue)
    else:
        dp_q, dp_r = dpnp.linalg.qr(a, mode=mode)

        dp_q_queue = dp_q.get_array().sycl_queue
        dp_r_queue = dp_r.get_array().sycl_queue

        assert_sycl_queue_equal(dp_q_queue, expected_queue)
        assert_sycl_queue_equal(dp_r_queue, expected_queue)
//  [python]
def test_qr(shape, mode, device):
    dtype = dpnp.default_float_type(device)
    count_elems = numpy.prod(shape)
    a = dpnp.arange(count_elems, dtype=dtype, device=device).reshape(shape)

    expected_queue = a.get_array().sycl_queue

    if mode == "r":
        dp_r = dpnp.linalg.qr(a, mode=mode)
        dp_r_queue = dp_r.get_array().sycl_queue
        assert_sycl_queue_equal(dp_r_queue, expected_queue)
    else:
        dp_q, dp_r = dpnp.linalg.qr(a, mode=mode)

        dp_q_queue = dp_q.get_array().sycl_queue
        dp_r_queue = dp_r.get_array().sycl_queue

        assert_sycl_queue_equal(dp_q_queue, expected_queue)
        assert_sycl_queue_equal(dp_r_queue, expected_queue)
//  [python]
def test_svd(shape, full_matrices, compute_uv, device):
    dtype = dpnp.default_float_type(device)

    count_elems = numpy.prod(shape)
    dpnp_data = dpnp.arange(count_elems, dtype=dtype, device=device).reshape(
        shape
    )
    expected_queue = dpnp_data.get_array().sycl_queue

    if compute_uv:
        dpnp_u, dpnp_s, dpnp_vt = dpnp.linalg.svd(
            dpnp_data, full_matrices=full_matrices, compute_uv=compute_uv
        )

        dpnp_u_queue = dpnp_u.get_array().sycl_queue
        dpnp_vt_queue = dpnp_vt.get_array().sycl_queue
        dpnp_s_queue = dpnp_s.get_array().sycl_queue

        assert_sycl_queue_equal(dpnp_u_queue, expected_queue)
        assert_sycl_queue_equal(dpnp_vt_queue, expected_queue)
        assert_sycl_queue_equal(dpnp_s_queue, expected_queue)

    else:
        dpnp_s = dpnp.linalg.svd(
            dpnp_data, full_matrices=full_matrices, compute_uv=compute_uv
        )
        dpnp_s_queue = dpnp_s.get_array().sycl_queue

        assert_sycl_queue_equal(dpnp_s_queue, expected_queue)
//  [python]
def test_slogdet(shape, is_empty, device):
    if is_empty:
        numpy_x = numpy.empty(shape, dtype=dpnp.default_float_type(device))
    else:
        count_elem = numpy.prod(shape)
        numpy_x = numpy.arange(
            1, count_elem + 1, dtype=dpnp.default_float_type(device)
        ).reshape(shape)

    dpnp_x = dpnp.array(numpy_x, device=device)

    sign_result, logdet_result = dpnp.linalg.slogdet(dpnp_x)
    sign_expected, logdet_expected = numpy.linalg.slogdet(numpy_x)
    assert_allclose(logdet_expected, logdet_result, rtol=1e-3, atol=1e-4)
    assert_allclose(sign_expected, sign_result)

    sign_queue = sign_result.sycl_queue
    logdet_queue = logdet_result.sycl_queue

    assert_sycl_queue_equal(sign_queue, dpnp_x.sycl_queue)
    assert_sycl_queue_equal(logdet_queue, dpnp_x.sycl_queue)
//  [python]
def size(self, matrix):
        """Returns the number of elements in the matrix.
        """
        return numpy.prod(self.shape(matrix))
//  [python]
def fl2norm2(xf, axis=(0, 1)):
    r"""
    Compute the squared :math:`\ell_2` norm in the DFT domain, taking
    into account the unnormalised DFT scaling, i.e. given the DFT of a
    multi-dimensional array computed via :func:`fftn`, return the
    squared :math:`\ell_2` norm of the original array.
    Parameters
    ----------
    xf : array_like
      Input array
    axis : sequence of ints, optional (default (0,1))
      Axes on which the input is in the frequency domain
    Returns
    -------
    x : float
      :math:`\|\mathbf{x}\|_2^2` where the input array is the result of
      applying :func:`fftn` to the specified axes of multi-dimensional
      array :math:`\mathbf{x}`
    """

    xfs = xf.shape
    return (linalg.norm(xf)**2)/n.prod(np.array([xfs[k] for k in axis]))
//  [python]
def fl2norm2(xf, axis=(0, 1)):
    r"""
    Compute the squared :math:`\ell_2` norm in the DFT domain, taking
    into account the unnormalised DFT scaling, i.e. given the DFT of a
    multi-dimensional array computed via :func:`fftn`, return the
    squared :math:`\ell_2` norm of the original array.
    Parameters
    ----------
    xf : array_like
      Input array
    axis : sequence of ints, optional (default (0,1))
      Axes on which the input is in the frequency domain
    Returns
    -------
    x : float
      :math:`\|\mathbf{x}\|_2^2` where the input array is the result of
      applying :func:`fftn` to the specified axes of multi-dimensional
      array :math:`\mathbf{x}`
    """

    xfs = xf.shape
    return (linalg.norm(xf)**2)/np.prod(np.array([xfs[k] for k in axis]))
//  [python]
def rfl2norm2(xf, xs, axis=(0, 1)):
    r"""
    Compute the squared :math:`\ell_2` norm in the DFT domain, taking
    into account the unnormalised DFT scaling, i.e. given the DFT of a
    multi-dimensional array computed via :func:`rfftn`, return the
    squared :math:`\ell_2` norm of the original array.
    Parameters
    ----------
    xf : array_like
      Input array
    xs : sequence of ints
      Shape of original array to which :func:`rfftn` was applied to
      obtain the input array
    axis : sequence of ints, optional (default (0,1))
      Axes on which the input is in the frequency domain
    Returns
    -------
    x : float
      :math:`\|\mathbf{x}\|_2^2` where the input array is the result of
      applying :func:`rfftn` to the specified axes of multi-dimensional
      array :math:`\mathbf{x}`
    """

    scl = 1.0 / n.prod(n.array([xs[k] for k in axis]))
    slc0 = (slice(None),)*axis[-1]
    nrm0 = linalg.norm(xf[slc0 + (0,)])
    idx1 = (xs[axis[-1]]+1)//2
    nrm1 = linalg.norm(xf[slc0 + (slice(1, idx1),)])
    if xs[axis[-1]] % 2 == 0:
        nrm2 = linalg.norm(xf[slc0 + (slice(-1, None),)])
    else:
        nrm2 = 0.0
    return scl*(nrm0**2 + 2.0*nrm1**2 + nrm2**2)
//  [python]
def rfl2norm2(xf, xs, axis=(0, 1)):
    r"""
    Compute the squared :math:`\ell_2` norm in the DFT domain, taking
    into account the unnormalised DFT scaling, i.e. given the DFT of a
    multi-dimensional array computed via :func:`rfftn`, return the
    squared :math:`\ell_2` norm of the original array.
    Parameters
    ----------
    xf : array_like
      Input array
    xs : sequence of ints
      Shape of original array to which :func:`rfftn` was applied to
      obtain the input array
    axis : sequence of ints, optional (default (0,1))
      Axes on which the input is in the frequency domain
    Returns
    -------
    x : float
      :math:`\|\mathbf{x}\|_2^2` where the input array is the result of
      applying :func:`rfftn` to the specified axes of multi-dimensional
      array :math:`\mathbf{x}`
    """

    scl = 1.0 / np.prod(np.array([xs[k] for k in axis]))
    slc0 = (slice(None),)*axis[-1]
    nrm0 = linalg.norm(xf[slc0 + (0,)])
    idx1 = (xs[axis[-1]]+1)//2
    nrm1 = linalg.norm(xf[slc0 + (slice(1, idx1),)])
    if xs[axis[-1]] % 2 == 0:
        nrm2 = linalg.norm(xf[slc0 + (slice(-1, None),)])
    else:
        nrm2 = 0.0
    return scl*(nrm0**2 + 2.0*nrm1**2 + nrm2**2)
//  [python]
def __eval_f1(self, solution: FloatSolution, g: float) -> float:
        f = 1.0 + g
        mult = n.prod([cos(v * pi / 2.0) for v in solution.variables[: self.M - 1]])

        return f * mult
//  [python]
def __eval_f1(self, solution: FloatSolution, g: float) -> float:
        f = 1.0 + g
        mult = numpy.prod([cos(v * pi / 2.0) for v in solution.variables[: self.M - 1]])

        return f * mult
//  [python]
def toggle_pruning(self, enable):
        """Enable or remove pruning.

        Args:
            enable: if we enable the pruning or not

        """

        # Maximum number of active neurons (i.e., corresponding weight != 0)

        # Go through all the convolution layers
        for layer in (self.conv1, self.conv2, self.conv3):
            s = layer.weight.shape

            # Compute fan-in (number of inputs to a neuron)
            # and fan-out (number of neurons in the layer)
            layer_size = [s[0], n.prod(s[1:])]

            # The number of input neurons (fan-in) is the product of
            # the kernel width x height x inChannels.
            if layer_size[1] > self.n_active:
                if enable:
                    # This will create a forward hook to create a mask tensor that is multiplied
                    # with the weights during forward. The mask will contain 0s or 1s
                    prune.l1_unstructured(
                        layer, "weight", (layer_size[1] - self.n_active) * layer_size[0]
                    )
                else:
                    # When disabling pruning, the mask is multiplied with the weights
                    # and the result is stored in the weights member
                    prune.remove(layer, "weight")
//  [python]
def toggle_pruning(self, enable):
        """Enable or remove pruning.

        Args:
            enable: if we enable the pruning or not

        """

        # Maximum number of active neurons (i.e., corresponding weight != 0)

        # Go through all the convolution layers
        for layer in (self.conv1, self.conv2, self.conv3):
            s = layer.weight.shape

            # Compute fan-in (number of inputs to a neuron)
            # and fan-out (number of neurons in the layer)
            layer_size = [s[0], numpy.prod(s[1:])]

            # The number of input neurons (fan-in) is the product of
            # the kernel width x height x inChannels.
            if layer_size[1] > self.n_active:
                if enable:
                    # This will create a forward hook to create a mask tensor that is multiplied
                    # with the weights during forward. The mask will contain 0s or 1s
                    prune.l1_unstructured(
                        layer, "weight", (layer_size[1] - self.n_active) * layer_size[0]
                    )
                else:
                    # When disabling pruning, the mask is multiplied with the weights
                    # and the result is stored in the weights member
                    prune.remove(layer, "weight")
//  [python]
def __init__(self, input_output, activation_function, n_bits=2, disable_bit_check=False):
        super().__init__()

        self.act = activation_function()
        self.fc1 = nn.Linear(input_output, input_output)

        # Create pre-quantized weights
        # Note the weights in the network are not integers, but uniformly spaced float values
        # that are selected from a discrete set
        weight_scale = 1.5

        n_bits_weights = n_bits

        # Generate the pattern 0, 1, ..., 2^N-1, 0, 1, .. 2^N-1, 0, 1..
        all_weights = numpy.mod(numpy.arange(n.prod(self.fc1.weight.shape)), 2**n_bits_weights)

        # Shuffle the pattern and reshape to weight shape
        numpy.random.shuffle(all_weights)
        int_weights = all_weights.reshape(self.fc1.weight.shape)

        # A check that is used to ensure this class is used correctly
        # but we may want to disable it to check that the QAT import catches the error
        if not disable_bit_check:
            # Ensure we have the correct max/min that produces the correct scale in Quantized Array
            assert numpy.max(int_weights) - numpy.min(int_weights) == (2**n_bits_weights - 1)

        # We want signed weights, so offset the generated weights
        int_weights = int_weights - 2 ** (n_bits_weights - 1)

        # Initialize with scaled float weights
        self.fc1.weight.data = torch.from_numpy(int_weights * weight_scale).float()

        self.n_bits = n_bits
//  [python]
def __init__(self, input_output, activation_function, n_bits=2, disable_bit_check=False):
        super().__init__()

        self.act = activation_function()
        self.fc1 = nn.Linear(input_output, input_output)

        # Create pre-quantized weights
        # Note the weights in the network are not integers, but uniformly spaced float values
        # that are selected from a discrete set
        weight_scale = 1.5

        n_bits_weights = n_bits

        # Generate the pattern 0, 1, ..., 2^N-1, 0, 1, .. 2^N-1, 0, 1..
        all_weights = numpy.mod(numpy.arange(numpy.prod(self.fc1.weight.shape)), 2**n_bits_weights)

        # Shuffle the pattern and reshape to weight shape
        numpy.random.shuffle(all_weights)
        int_weights = all_weights.reshape(self.fc1.weight.shape)

        # A check that is used to ensure this class is used correctly
        # but we may want to disable it to check that the QAT import catches the error
        if not disable_bit_check:
            # Ensure we have the correct max/min that produces the correct scale in Quantized Array
            assert numpy.max(int_weights) - numpy.min(int_weights) == (2**n_bits_weights - 1)

        # We want signed weights, so offset the generated weights
        int_weights = int_weights - 2 ** (n_bits_weights - 1)

        # Initialize with scaled float weights
        self.fc1.weight.data = torch.from_numpy(int_weights * weight_scale).float()

        self.n_bits = n_bits
//  [python]
def __eval_f1(self, solution: FloatSolution, g: float, ft: float) -> float:
        f = 1.0 + g
        mult = n.prod([cos(pow(v, ft) * pi / 2.0) for v in solution.variables[: self.M - 1]])

        return f * mult
//  [python]
def __eval_f1(self, solution: FloatSolution, g: float, ft: float) -> float:
        f = 1.0 + g
        mult = numpy.prod([cos(pow(v, ft) * pi / 2.0) for v in solution.variables[: self.M - 1]])

        return f * mult
//  [python]
def __eval_fk(self, solution: FloatSolution, g: float, k: int) -> float:
        f = 1.0 + g
        aux = sin((solution.variables[self.M - k] * pi) / 2.0)
        mult = n.prod([cos(v * pi / 2.0) for v in solution.variables[: self.M - k]])

        return f * mult * aux
//  [python]
def __eval_fk(self, solution: FloatSolution, g: float, k: int) -> float:
        f = 1.0 + g
        aux = sin((solution.variables[self.M - k] * pi) / 2.0)
        mult = numpy.prod([cos(v * pi / 2.0) for v in solution.variables[: self.M - k]])

        return f * mult * aux
//  [python]
def _zpklp2hp(z, p, k, wo=1.0):
    r"""
    Transform a lowpass filter prototype to a highpass filter.

    Return an analog high-pass filter with cutoff frequency `wo`
    from an analog low-pass filter prototype with unity cutoff frequency,
    using zeros, poles, and gain ('zpk') representation.

    Parameters
    ----------
    z : ndarray
        Zeros of the analog IIR filter transfer function.
    p : ndarray
        Poles of the analog IIR filter transfer function.
    k : float
        System gain of the analog IIR filter transfer function.
    wo : float
        Desired cutoff, as angular frequency (e.g. rad/s).
        Defaults to no change.

    Returns
    -------
    z : ndarray
        Zeros of the transformed high-pass filter transfer function.
    p : ndarray
        Poles of the transformed high-pass filter transfer function.
    k : float
        System gain of the transformed high-pass filter.

    Notes
    -----
    This is derived from the s-plane substitution

    .. math:: s \rightarrow \frac{\omega_0}{s}

    This maintains symmetry of the lowpass and highpass responses on a
    logarithmic scale.

    """
    z = atleast_1d(z)
    p = atleast_1d(p)
    wo = float(wo)

    degree = _relative_degree(z, p)

    # Invert positions radially about unit circle to convert LPF to HPF
    # Scale all points radially from origin to shift cutoff frequency
    z_hp = wo / z
    p_hp = wo / p

    # If lowpass had zeros at infinity, inverting moves them to origin.
    z_hp = append(z_hp, zeros(degree))

    # Cancel out gain change caused by inversion
    k_hp = k * real(n.prod(-z) / n.prod(-p))

    return z_hp, p_hp, k_hp
//  [python]
def _zpklp2hp(z, p, k, wo=1.0):
    r"""
    Transform a lowpass filter prototype to a highpass filter.

    Return an analog high-pass filter with cutoff frequency `wo`
    from an analog low-pass filter prototype with unity cutoff frequency,
    using zeros, poles, and gain ('zpk') representation.

    Parameters
    ----------
    z : ndarray
        Zeros of the analog IIR filter transfer function.
    p : ndarray
        Poles of the analog IIR filter transfer function.
    k : float
        System gain of the analog IIR filter transfer function.
    wo : float
        Desired cutoff, as angular frequency (e.g. rad/s).
        Defaults to no change.

    Returns
    -------
    z : ndarray
        Zeros of the transformed high-pass filter transfer function.
    p : ndarray
        Poles of the transformed high-pass filter transfer function.
    k : float
        System gain of the transformed high-pass filter.

    Notes
    -----
    This is derived from the s-plane substitution

    .. math:: s \rightarrow \frac{\omega_0}{s}

    This maintains symmetry of the lowpass and highpass responses on a
    logarithmic scale.

    """
    z = atleast_1d(z)
    p = atleast_1d(p)
    wo = float(wo)

    degree = _relative_degree(z, p)

    # Invert positions radially about unit circle to convert LPF to HPF
    # Scale all points radially from origin to shift cutoff frequency
    z_hp = wo / z
    p_hp = wo / p

    # If lowpass had zeros at infinity, inverting moves them to origin.
    z_hp = append(z_hp, zeros(degree))

    # Cancel out gain change caused by inversion
    k_hp = k * real(prod(-z) / prod(-p))

    return z_hp, p_hp, k_hp
//  [python]
def __eval_fk(self, solution: FloatSolution, g: float, k: int, ft: float) -> float:
        f = 1.0 + g

        mult = n.prod([cos(pow(v, ft) * pi / 2.0) for v in solution.variables[: self.M - k]])
        yy = pow(solution.variables[self.M - k], ft)
        mult *= sin(yy * pi / 2.0)

        return f * mult
//  [python]
def __eval_fk(self, solution: FloatSolution, g: float, k: int, ft: float) -> float:
        f = 1.0 + g

        mult = numpy.prod([cos(pow(v, ft) * pi / 2.0) for v in solution.variables[: self.M - k]])
        yy = pow(solution.variables[self.M - k], ft)
        mult *= sin(yy * pi / 2.0)

        return f * mult
//  [python]
def traced_conv_reference(pad_metadata, data_top_left_indices, conv_params, input_nchw_shape):
    assert len(conv_params) == 10
    output_channels, input_channels, filter_h, filter_w, stride_h, stride_w, pad_h, pad_w, dilation, groups = [
        conv_params[i] for i in range(10)
    ]
    # unpadded tensor
    input_tensor = []
    assert len(input_nchw_shape) == 4
    input_n, input_c, input_h, input_w = input_nchw_shape
    assert input_c == 1  # Ref done for channel size = 1
    input_volume = n.prod(input_nchw_shape)

    # Initialize tensor with data
    # Inserting sequential integer data
    for val in range(1, input_volume + 1):
        input_tensor.append(val)
    input_pyt_tensor = torch.tensor(input_tensor)
    input_pyt_tensor = torch.reshape(input_pyt_tensor, input_nchw_shape)

    # Construct the padded tensor using pad_metadata
    input_padded_tensor = []
    input_padded_width = input_w + (2 * pad_w)
    input_padded_height = input_h + (2 * pad_h)
    input_padded_volume = input_n * input_padded_height * input_padded_width
    input_tensor_idx = 0
    assert len(pad_metadata) == input_padded_volume
    for i in range(input_padded_volume):
        if pad_metadata[i]:
            input_padded_tensor.append(0)
        else:
            input_padded_tensor.append(input_tensor[input_tensor_idx])
            input_tensor_idx += 1

    assert len(input_padded_tensor) == input_padded_volume
    input_padded_pyt_tensor = torch.tensor(input_padded_tensor).reshape(
        [1, input_n * input_padded_height, input_padded_width]
    )
    filter_volume = filter_h * filter_w
    # Initializing filters with all 1s
    filter_pyt_tensor = torch.full((1, 1, filter_h, filter_w), 1)

    output_tensor = []
    # run conv over padded tensor using data_top_left_indices
    for i in data_top_left_indices:
        i_bh = (int)(i / input_padded_width)
        i_w = (int)(i % input_padded_width)
        output_tensor.append(
            torch.dot(
                input_padded_pyt_tensor[:, i_bh : i_bh + filter_h, i_w : i_w + filter_w].reshape(-1),
                filter_pyt_tensor.reshape(-1),
            )
        )

    output_pyt_tensor = torch.tensor(output_tensor)
    # run conv pytorch
    out_golden_pyt_tensor = torch.nn.functional.conv2d(
        input_pyt_tensor, filter_pyt_tensor, stride=(stride_h, stride_w), padding=(pad_h, pad_w)
    )
    assert n.prod(output_pyt_tensor.size()) == n.prod(out_golden_pyt_tensor.size())
    output_pyt_tensor = torch.reshape(output_pyt_tensor, out_golden_pyt_tensor.size())

    # compare to pytorch
    passing_pcc, output_pcc = comp_equal(out_golden_pyt_tensor, output_pyt_tensor)
    logger.debug(f"Passing={passing_pcc}")
    logger.debug(f"Output pcc={output_pcc}")
    assert passing_pcc

    return
//  [python]
def traced_conv_reference(pad_metadata, data_top_left_indices, conv_params, input_nchw_shape):
    assert len(conv_params) == 10
    output_channels, input_channels, filter_h, filter_w, stride_h, stride_w, pad_h, pad_w, dilation, groups = [
        conv_params[i] for i in range(10)
    ]
    # unpadded tensor
    input_tensor = []
    assert len(input_nchw_shape) == 4
    input_n, input_c, input_h, input_w = input_nchw_shape
    assert input_c == 1  # Ref done for channel size = 1
    input_volume = numpy.prod(input_nchw_shape)

    # Initialize tensor with data
    # Inserting sequential integer data
    for val in range(1, input_volume + 1):
        input_tensor.append(val)
    input_pyt_tensor = torch.tensor(input_tensor)
    input_pyt_tensor = torch.reshape(input_pyt_tensor, input_nchw_shape)

    # Construct the padded tensor using pad_metadata
    input_padded_tensor = []
    input_padded_width = input_w + (2 * pad_w)
    input_padded_height = input_h + (2 * pad_h)
    input_padded_volume = input_n * input_padded_height * input_padded_width
    input_tensor_idx = 0
    assert len(pad_metadata) == input_padded_volume
    for i in range(input_padded_volume):
        if pad_metadata[i]:
            input_padded_tensor.append(0)
        else:
            input_padded_tensor.append(input_tensor[input_tensor_idx])
            input_tensor_idx += 1

    assert len(input_padded_tensor) == input_padded_volume
    input_padded_pyt_tensor = torch.tensor(input_padded_tensor).reshape(
        [1, input_n * input_padded_height, input_padded_width]
    )
    filter_volume = filter_h * filter_w
    # Initializing filters with all 1s
    filter_pyt_tensor = torch.full((1, 1, filter_h, filter_w), 1)

    output_tensor = []
    # run conv over padded tensor using data_top_left_indices
    for i in data_top_left_indices:
        i_bh = (int)(i / input_padded_width)
        i_w = (int)(i % input_padded_width)
        output_tensor.append(
            torch.dot(
                input_padded_pyt_tensor[:, i_bh : i_bh + filter_h, i_w : i_w + filter_w].reshape(-1),
                filter_pyt_tensor.reshape(-1),
            )
        )

    output_pyt_tensor = torch.tensor(output_tensor)
    # run conv pytorch
    out_golden_pyt_tensor = torch.nn.functional.conv2d(
        input_pyt_tensor, filter_pyt_tensor, stride=(stride_h, stride_w), padding=(pad_h, pad_w)
    )
    assert numpy.prod(output_pyt_tensor.size()) == numpy.prod(out_golden_pyt_tensor.size())
    output_pyt_tensor = torch.reshape(output_pyt_tensor, out_golden_pyt_tensor.size())

    # compare to pytorch
    passing_pcc, output_pcc = comp_equal(out_golden_pyt_tensor, output_pyt_tensor)
    logger.debug(f"Passing={passing_pcc}")
    logger.debug(f"Output pcc={output_pcc}")
    assert passing_pcc

    return
//  [python]
def topographic_error(self, data):
        """Returns the topographic error computed by finding
        the best-matching and second-best-matching neuron in the map
        for each input and then evaluating the positions.

        A sample for which these two nodes are not adjacent counts as
        an error. The topographic error is given by the
        the total number of errors divided by the total of samples.

        If the topographic error is 0, no error occurred.
        If 1, the topology was not preserved for any of the samples."""
        self._check_input_len(data)
        total_neurons = n.prod(self._activation_map.shape)
        if total_neurons == 1:
            warn('The topographic error is not defined for a 1-by-1 map.')
            return nan
        if self.topology == 'hexagonal':
            return self._topographic_error_hexagonal(data)
        else:
            return self._topographic_error_rectangular(data)
//  [python]
def topographic_error(self, data):
        """Returns the topographic error computed by finding
        the best-matching and second-best-matching neuron in the map
        for each input and then evaluating the positions.

        A sample for which these two nodes are not adjacent counts as
        an error. The topographic error is given by the
        the total number of errors divided by the total of samples.

        If the topographic error is 0, no error occurred.
        If 1, the topology was not preserved for any of the samples."""
        self._check_input_len(data)
        total_neurons = prod(self._activation_map.shape)
        if total_neurons == 1:
            warn('The topographic error is not defined for a 1-by-1 map.')
            return nan
        if self.topology == 'hexagonal':
            return self._topographic_error_hexagonal(data)
        else:
            return self._topographic_error_rectangular(data)
//  [python]
def frame(self, x, frame_length, hop_length):
        '''
            Slices a data array into (overlapping) frames.
            args:
                x : array to frame
                frame_length : length of frame
                hop_length : Number of steps to advance between frames
            return : A framed view of `x`
        '''
        if x.shape[-1] < frame_length:
            raise Exception(
                "Input is too short (n={:d})"
                " for frame_length={:d}".format(x.shape[-1], frame_length)
            )
        x = np.asfortranarray(x)
        n_frames = 1 + (x.shape[-1] - frame_length) // hop_length
        strides = np.asarray(x.strides)
        new_stride = np.multiply.reduce(strides[strides > 0] // x.itemsize) * x.itemsize
        shape = list(x.shape)[:-1] + [frame_length, n_frames]
        strides = list(strides) + [hop_length * new_stride]
        return np.lib.stride_tricks.as_strided(x, shape=shape, strides=strides)
//  [python]
def frame(self, x, frame_length, hop_length):
        '''
            Slices a data array into (overlapping) frames.
            args:
                x : array to frame
                frame_length : length of frame
                hop_length : Number of steps to advance between frames
            return : A framed view of `x`
        '''
        if x.shape[-1] < frame_length:
            raise Exception(
                "Input is too short (n={:d})"
                " for frame_length={:d}".format(x.shape[-1], frame_length)
            )
        x = np.asfortranarray(x)
        n_frames = 1 + (x.shape[-1] - frame_length) // hop_length
        strides = np.asarray(x.strides)
        new_stride = np.prod(strides[strides > 0] // x.itemsize) * x.itemsize
        shape = list(x.shape)[:-1] + [frame_length, n_frames]
        strides = list(strides) + [hop_length * new_stride]
        return np.lib.stride_tricks.as_strided(x, shape=shape, strides=strides)
//  [python]
def set_linked_probs(
        self,
        linked_statements: List[LinkedStatement],
    ) -> None:
        """Sets the belief probabilities for a list of linked INDRA Statements.

        The list of LinkedStatement objects is assumed to come from the
        MechanismLinker. The belief probability of the inferred Statement is
        assigned the joint probability of its source Statements.

        Parameters
        ----------
        linked_statements :
            A list of INDRA LinkedStatements whose belief scores are to
            be calculated. The belief attribute of the inferred Statement in
            the LinkedStatement object is updated by this function.
        """
        for st in linked_statements:
            source_probs = [s.belief for s in st.source_stmts]
            st.inferred_stmt.belief = n.prod(source_probs)
//  [python]
def set_linked_probs(
        self,
        linked_statements: List[LinkedStatement],
    ) -> None:
        """Sets the belief probabilities for a list of linked INDRA Statements.

        The list of LinkedStatement objects is assumed to come from the
        MechanismLinker. The belief probability of the inferred Statement is
        assigned the joint probability of its source Statements.

        Parameters
        ----------
        linked_statements :
            A list of INDRA LinkedStatements whose belief scores are to
            be calculated. The belief attribute of the inferred Statement in
            the LinkedStatement object is updated by this function.
        """
        for st in linked_statements:
            source_probs = [s.belief for s in st.source_stmts]
            st.inferred_stmt.belief = numpy.prod(source_probs)
//  [python]
def _NumpyPerImageWhitening(self, x):
    num_pixels = np.multiply.reduce(x.shape)
    x2 = np.square(x).astype(np.float32)
    mn = np.mean(x)
    vr = np.mean(x2) - (mn * mn)
    stddev = max(math.sqrt(vr), 1.0 / math.sqrt(num_pixels))

    y = x.astype(np.float32)
    y -= mn
    y /= stddev
    return y
//  [python]
def _NumpyPerImageWhitening(self, x):
    num_pixels = np.prod(x.shape)
    x2 = np.square(x).astype(np.float32)
    mn = np.mean(x)
    vr = np.mean(x2) - (mn * mn)
    stddev = max(math.sqrt(vr), 1.0 / math.sqrt(num_pixels))

    y = x.astype(np.float32)
    y -= mn
    y /= stddev
    return y
//  [python]
def testBasic(self):
    x_shape = [13, 9, 3]
    x_np = np.arange(0, np.prod(x_shape), dtype=np.int32).reshape(x_shape)
    y_np = self._NumpyPerImageWhitening(x_np)

    with self.test_session(use_gpu=True):
      x = constant_op.constant(x_np, shape=x_shape)
      y = image_ops.per_image_standardization(x)
      y_tf = y.eval()
      self.assertAllClose(y_tf, y_np, atol=1e-4)
//  [python]
def __init__(self, logbf, laguerre_n, Lambda0, prior=-0.5,
                 name='count posterior', unit='signals/experiment',
                 texunit=r'\mathrm{signals}/\mathrm{experiment}',
                 texsymb=r'\Lambda_1'):
        '''
        Parameters
        ----------
        logbf : array_like
            logs of normalized foreground over background pdf ratios of events
        laguerre_n: int
            degree of generalized Laguerre polynomial for quadrature formula
        Lambda0 : float
            background rate (default=len(bayesfac))
        prior : float or count_posterior, optional
            prior distribution power law of improper prior if float
            or count posterior distribution if count_posterior
            (default=-0.5: Jeffreys prior)
        '''
        super(count_posterior, self).__init__(a=0.0, b=numpy.inf, name=name,
                                              unit=unit, texunit=texunit,
                                              texsymb=texsymb)
        self.Lambda0 = Lambda0
        # weighted Bayes factor
        self.k = numpy.exp(numpy.array(logbf)) / self.Lambda0

        # power-law priors
        self.alpha = prior
        if prior == 0:
            self.prior = lambda x: 1.0
        elif prior > 0:
            self.prior = lambda x: x ** prior
        else:
            # regularize at x = 0
            self.prior = lambda x: (x + self.xtol) ** prior

        # pre-compute Gaussian-Generalized-Laguerre quadrature
        # abscissas and weights, along with pdf at these abscissas
        self.x, w = ssp.la_roots(laguerre_n, self.alpha)
        self.p = numpy.array([ww * n.prod(1.0 + self.k * xx)
                              for xx, ww in zip(self.x, w)])
        self.norm = 1.0 / sum(self.p)
        self.p *= self.norm
//  [python]
def __init__(self, logbf, laguerre_n, Lambda0, prior=-0.5,
                 name='count posterior', unit='signals/experiment',
                 texunit=r'\mathrm{signals}/\mathrm{experiment}',
                 texsymb=r'\Lambda_1'):
        '''
        Parameters
        ----------
        logbf : array_like
            logs of normalized foreground over background pdf ratios of events
        laguerre_n: int
            degree of generalized Laguerre polynomial for quadrature formula
        Lambda0 : float
            background rate (default=len(bayesfac))
        prior : float or count_posterior, optional
            prior distribution power law of improper prior if float
            or count posterior distribution if count_posterior
            (default=-0.5: Jeffreys prior)
        '''
        super(count_posterior, self).__init__(a=0.0, b=numpy.inf, name=name,
                                              unit=unit, texunit=texunit,
                                              texsymb=texsymb)
        self.Lambda0 = Lambda0
        # weighted Bayes factor
        self.k = numpy.exp(numpy.array(logbf)) / self.Lambda0

        # power-law priors
        self.alpha = prior
        if prior == 0:
            self.prior = lambda x: 1.0
        elif prior > 0:
            self.prior = lambda x: x ** prior
        else:
            # regularize at x = 0
            self.prior = lambda x: (x + self.xtol) ** prior

        # pre-compute Gaussian-Generalized-Laguerre quadrature
        # abscissas and weights, along with pdf at these abscissas
        self.x, w = ssp.la_roots(laguerre_n, self.alpha)
        self.p = numpy.array([ww * numpy.prod(1.0 + self.k * xx)
                              for xx, ww in zip(self.x, w)])
        self.norm = 1.0 / sum(self.p)
        self.p *= self.norm
//  [python]
def testWholeImageBoundingBox(self):
    height = 40
    width = 50
    image_size = [height, width, 1]
    bounding_box = [0.0, 0.0, 1.0, 1.0]
    image = np.arange(
        0, np.multiply.reduce(image_size), dtype=np.int32).reshape(image_size)
    self._testSampleDistortedBoundingBox(
        image,
        bounding_box,
        min_object_covered=0.1,
        aspect_ratio_range=(0.75, 1.33),
        area_range=(0.05, 1.0))
//  [python]
def testWholeImageBoundingBox(self):
    height = 40
    width = 50
    image_size = [height, width, 1]
    bounding_box = [0.0, 0.0, 1.0, 1.0]
    image = np.arange(
        0, np.prod(image_size), dtype=np.int32).reshape(image_size)
    self._testSampleDistortedBoundingBox(
        image,
        bounding_box,
        min_object_covered=0.1,
        aspect_ratio_range=(0.75, 1.33),
        area_range=(0.05, 1.0))
//  [python]
def score_evidence_list(
        self,
        evidences: List[Evidence],
    ) -> float:
        """Return belief score given a list of supporting evidences.

        Parameters
        ----------
        evidences :
            List of evidences to use for calculating a statement's belief.

        Returns
        -------
        :
            Belief value based on the evidences.
        """
        def _score(evidences):
            if not evidences:
                return 0
            # Collect all unique sources
            sources = [ev.source_api for ev in evidences]
            uniq_sources = numpy.unique(sources)
            # Calculate the systematic error factors given unique sources
            syst_factors = {s: self.prior_probs['syst'][s]
                            for s in uniq_sources}
            # Calculate the random error factors for each source
            rand_factors = {k: [] for k in uniq_sources}
            for ev in evidences:
                rand_factors[ev.source_api].append(
                    evidence_random_noise_prior(
                        ev,
                        self.prior_probs['rand'],
                        self.subtype_probs))
            # The probability of incorrectness is the product of the
            # source-specific probabilities
            neg_prob_prior = 1
            for s in uniq_sources:
                neg_prob_prior *= (syst_factors[s] +
                                   numpy.prod(rand_factors[s]))
            # Finally, the probability of correctness is one minus incorrect
            prob_prior = 1 - neg_prob_prior
            return prob_prior
        # Split evidence into positive and negative and score
        pos_evidence = [ev for ev in evidences if
                        not ev.epistemics.get('negated')]
        neg_evidence = [ev for ev in evidences if
                        ev.epistemics.get('negated')]
        pp = _score(pos_evidence)
        np = _score(neg_evidence)
        # The basic assumption is that the positive and negative evidence
        # can't simultaneously be correct.
        # There are two cases to consider. (1) If the positive evidence is
        # incorrect then there is no Statement and the belief should be 0,
        # irrespective of the negative evidence.
        # (2) If the positive evidence is correct and the negative evidence
        # is incorrect.
        # This amounts to the following formula:
        # 0 * (1-pp) + 1 * (pp * (1-np)) which we simplify below
        score = pp * (1 - np)
        return score
//  [python]
def forward(self, x, hw_shape):
        B, N, _ = x.shape

        if self.pool_first:
            if self.mode == "conv_unshared":
                fold_dim = 1
            else:
                fold_dim = self.num_heads
            x = x.reshape(B, N, fold_dim, -1).permute(0, 2, 1, 3)
            q = k = v = x
        else:
            assert self.mode != "conv_unshared"

            qkv = (
                self.qkv(x).reshape(B, N, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4)
            )
            q, k, v = qkv[0], qkv[1], qkv[2]

        q, q_shape = attention_pool(
            q,
            self.pool_q,
            hw_shape,
            has_cls_embed=self.has_cls_embed,
            norm=getattr(self, "norm_q", None),
        )
        k, k_shape = attention_pool(
            k,
            self.pool_k,
            hw_shape,
            has_cls_embed=self.has_cls_embed,
            norm=getattr(self, "norm_k", None),
        )
        v, v_shape = attention_pool(
            v,
            self.pool_v,
            hw_shape,
            has_cls_embed=self.has_cls_embed,
            norm=getattr(self, "norm_v", None),
        )

        if self.pool_first:
            q_N = n.prod(q_shape) + 1 if self.has_cls_embed else n.prod(q_shape)
            k_N = n.prod(k_shape) + 1 if self.has_cls_embed else n.prod(k_shape)
            v_N = n.prod(v_shape) + 1 if self.has_cls_embed else n.prod(v_shape)

            q = q.permute(0, 2, 1, 3).reshape(B, q_N, -1)
            q = self.q(q).reshape(B, q_N, self.num_heads, -1).permute(0, 2, 1, 3)

            v = v.permute(0, 2, 1, 3).reshape(B, v_N, -1)
            v = self.v(v).reshape(B, v_N, self.num_heads, -1).permute(0, 2, 1, 3)

            k = k.permute(0, 2, 1, 3).reshape(B, k_N, -1)
            k = self.k(k).reshape(B, k_N, self.num_heads, -1).permute(0, 2, 1, 3)

        N = q.shape[2]
        attn = (q * self.scale) @ k.transpose(-2, -1)
        if self.rel_pos_spatial:
            attn = cal_rel_pos_spatial(
                attn,
                q,
                self.has_cls_embed,
                q_shape,
                k_shape,
                self.rel_pos_h,
                self.rel_pos_w,
            )

        attn = attn.softmax(dim=-1)
        x = attn @ v

        if self.residual_pooling:
            if self.has_cls_embed:
                x[:, :, 1:, :] += q[:, :, 1:, :]
            else:
                x = x + q

        x = x.transpose(1, 2).reshape(B, -1, self.dim_out)
        x = self.proj(x)

        return x, q_shape
//  [python]
def forward(self, x, hw_shape):
        B, N, _ = x.shape

        if self.pool_first:
            if self.mode == "conv_unshared":
                fold_dim = 1
            else:
                fold_dim = self.num_heads
            x = x.reshape(B, N, fold_dim, -1).permute(0, 2, 1, 3)
            q = k = v = x
        else:
            assert self.mode != "conv_unshared"

            qkv = (
                self.qkv(x).reshape(B, N, 3, self.num_heads, -1).permute(2, 0, 3, 1, 4)
            )
            q, k, v = qkv[0], qkv[1], qkv[2]

        q, q_shape = attention_pool(
            q,
            self.pool_q,
            hw_shape,
            has_cls_embed=self.has_cls_embed,
            norm=getattr(self, "norm_q", None),
        )
        k, k_shape = attention_pool(
            k,
            self.pool_k,
            hw_shape,
            has_cls_embed=self.has_cls_embed,
            norm=getattr(self, "norm_k", None),
        )
        v, v_shape = attention_pool(
            v,
            self.pool_v,
            hw_shape,
            has_cls_embed=self.has_cls_embed,
            norm=getattr(self, "norm_v", None),
        )

        if self.pool_first:
            q_N = numpy.prod(q_shape) + 1 if self.has_cls_embed else numpy.prod(q_shape)
            k_N = numpy.prod(k_shape) + 1 if self.has_cls_embed else numpy.prod(k_shape)
            v_N = numpy.prod(v_shape) + 1 if self.has_cls_embed else numpy.prod(v_shape)

            q = q.permute(0, 2, 1, 3).reshape(B, q_N, -1)
            q = self.q(q).reshape(B, q_N, self.num_heads, -1).permute(0, 2, 1, 3)

            v = v.permute(0, 2, 1, 3).reshape(B, v_N, -1)
            v = self.v(v).reshape(B, v_N, self.num_heads, -1).permute(0, 2, 1, 3)

            k = k.permute(0, 2, 1, 3).reshape(B, k_N, -1)
            k = self.k(k).reshape(B, k_N, self.num_heads, -1).permute(0, 2, 1, 3)

        N = q.shape[2]
        attn = (q * self.scale) @ k.transpose(-2, -1)
        if self.rel_pos_spatial:
            attn = cal_rel_pos_spatial(
                attn,
                q,
                self.has_cls_embed,
                q_shape,
                k_shape,
                self.rel_pos_h,
                self.rel_pos_w,
            )

        attn = attn.softmax(dim=-1)
        x = attn @ v

        if self.residual_pooling:
            if self.has_cls_embed:
                x[:, :, 1:, :] += q[:, :, 1:, :]
            else:
                x = x + q

        x = x.transpose(1, 2).reshape(B, -1, self.dim_out)
        x = self.proj(x)

        return x, q_shape