jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def get_nbytes_msg(sizedict, size=8):
    """
    :param sizedict: mapping name -> num_dimensions
    :returns: (size of the array in bytes, descriptive message)

    >>> get_nbytes_msg(dict(nsites=2, nbins=5))
    (80, '(nsites=2) * (nbins=5) * 8 bytes = 80 B')
    """
    nbytes = reduce(operator.mul, list(sizedict.values())) * size
    prod = ' * '.join('({}={:_d})'.format(k, int(v))
                      for k, v in sizedict.items())
    return nbytes, '%s * %d bytes = %s' % (prod, size, humansize(nbytes))
//  [python]
def get_nbytes_msg(sizedict, size=8):
    """
    :param sizedict: mapping name -> num_dimensions
    :returns: (size of the array in bytes, descriptive message)

    >>> get_nbytes_msg(dict(nsites=2, nbins=5))
    (80, '(nsites=2) * (nbins=5) * 8 bytes = 80 B')
    """
    nbytes = numpy.prod(list(sizedict.values())) * size
    prod = ' * '.join('({}={:_d})'.format(k, int(v))
                      for k, v in sizedict.items())
    return nbytes, '%s * %d bytes = %s' % (prod, size, humansize(nbytes))
//  [python]
def setUp(self):
        self.y = numpy.arange(
            numpy.multiply.reduce(self.shape), dtype=self.dtype).reshape(self.shape)
        self.xs = [self.y[s] for s in self.slices]
//  [python]
def setUp(self):
        self.y = numpy.arange(
            numpy.prod(self.shape), dtype=self.dtype).reshape(self.shape)
        self.xs = [self.y[s] for s in self.slices]
//  [python]
def make_CP_encoder(buffers):
    """create an encoder for CellProfiler data and numpy arrays (which will be
    stored in the input argument)"""

    def encoder(data, buffers=buffers):
        if isinstance(data, numpy.ndarray):
            #
            # Maybe it's nice to save memory by converting 64-bit to 32-bit
            # but the purpose here is to fix a bug on the Mac where a
            # 32-bit worker gets a 64-bit array or unsigned 32-bit array,
            # tries to use it for indexing and fails because the integer
            # is wider than a 32-bit pointer
            #
            info32 = numpy.iinfo(numpy.int32)
            if (
                data.dtype.kind == "i"
                and data.dtype.itemsize > 4
                or data.dtype.kind == "u"
                and data.dtype.itemsize >= 4
            ):
                if numpy.prod(data.shape) == 0 or (
                    numpy.min(data) >= info32.min and numpy.max(data) <= info32.max
                ):
                    data = data.astype(numpy.int32)
            idx = len(buffers)
            buffers.append(numpy.ascontiguousarray(data))
            dtype = str(data.dtype)
            return {
                "__ndarray__": True,
                "dtype": str(data.dtype),
                "shape": data.shape,
                "idx": idx,
            }
        if isinstance(data, numpy.generic):
            # http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html
            return data.astype(object)
        if isinstance(data, cellprofiler_core.utilities.grid.Grid):
            d = data.serialize()
            d["__CPGridInfo__"] = True
            return d
        if isinstance(data, memoryview):
            # arbitrary data
            idx = len(buffers)
            buffers.append(data)
            return {"__buffer__": True, "idx": idx}
        raise TypeError("%r of type %r is not JSON serializable" % (data, type(data)))

    return encoder
//  [python]
def write_vox(filename, s):
    fout = open(filename, 'wb')
    fout.write(struct.pack('I', s.dims[0]))
    fout.write(struct.pack('I', s.dims[1]))
    fout.write(struct.pack('I', s.dims[2]))
    fout.write(struct.pack('f', s.res))
    n_elems = numpy.prod(s.dims)
    fout.write(struct.pack('f'*16, *s.grid2world.flatten('F')))
    fout.write(struct.pack('f'*n_elems, *s.sdf.flatten('C')))
    if s.pdf is not None:
        fout.write(struct.pack('f'*n_elems, *s.pdf.flatten('C')))
    if s.noc is not None:
        fout.write(struct.pack('f'*n_elems*3, *s.noc.flatten('C')))
    if s.bbox is not None:
        fout.write(struct.pack('f'*n_elems, *s.bbox.flatten('C')))
    fout.close()
//  [python]
def write_vox(filename, s):
    fout = open(filename, 'wb')
    fout.write(struct.pack('I', s.dims[0]))
    fout.write(struct.pack('I', s.dims[1]))
    fout.write(struct.pack('I', s.dims[2]))
    fout.write(struct.pack('f', s.res))
    n_elems = np.prod(s.dims)
    fout.write(struct.pack('f'*16, *s.grid2world.flatten('F')))
    fout.write(struct.pack('f'*n_elems, *s.sdf.flatten('C')))
    if s.pdf is not None:
        fout.write(struct.pack('f'*n_elems, *s.pdf.flatten('C')))
    if s.noc is not None:
        fout.write(struct.pack('f'*n_elems*3, *s.noc.flatten('C')))
    if s.bbox is not None:
        fout.write(struct.pack('f'*n_elems, *s.bbox.flatten('C')))
    fout.close()
//  [python]
def numpy_flatten(x: numpy.ndarray, *, axis: int = 1) -> Tuple[numpy.ndarray]:
    """Flatten a tensor into a 2d array.

    See https://github.com/onnx/onnx/blob/main/docs/Changelog.md#Flatten-13.

    Args:
        x (numpy.ndarray): tensor to flatten
        axis (int): axis after which all dimensions will be flattened (axis=0 gives a 1D output)

    Returns:
        result: flattened tensor
    """
    output_shape: Sequence[SupportsIndex]
    output_shape = (*x.shape[0:axis], numpy.prod(x.shape[axis:]))

    return (numpy.reshape(x, output_shape),)
//  [python]
def get_total_subsampling_factor(self) -> float:
        """Get total subsampling factor."""
        if self.etype == "custom":
            return self.encoder.conv_subsampling_factor * int(
                n.prod(self.subsample)
            )
        else:
            return self.enc.conv_subsampling_factor * int(n.prod(self.subsample))
//  [python]
def get_total_subsampling_factor(self) -> float:
        """Get total subsampling factor."""
        if self.etype == "custom":
            return self.encoder.conv_subsampling_factor * int(
                numpy.prod(self.subsample)
            )
        else:
            return self.enc.conv_subsampling_factor * int(numpy.prod(self.subsample))
//  [python]
def add(self, output, target):
        output = output.cpu().squeeze().numpy()
        target = target.cpu().squeeze().numpy()

        if np.ndim(output) == 1:
            output = output[None]

        onehot = np.ndim(target) != 1
        assert output.shape[0] == target.shape[0], \
                'number of targets and outputs do not match'
        assert output.shape[1] == self.conf.shape[0], \
                'number of outputs does not match size of confusion matrix'
        assert not onehot or target.shape[1] == output.shape[1], \
                'target should be 1D Tensor or have size of output (one-hot)'
        if onehot:
            assert (target >= 0).all() and (target <= 1).all(), \
                    'in one-hot encoding, target values should be 0 or 1'
            assert (target.sum(1) == 1).all(), \
                    'multi-label setting is not supported'

        target = target.argmax(1) if onehot else target
        pred = output.argmax(1)

        target    = target.astype(np.int32)
        pred      = pred.astype(np.int32)
        conf_this = np.bincount(target * self.conf.shape[0] + pred,minlength=np.multiply.reduce(self.conf.shape))
        conf_this = conf_this.astype(self.conf.dtype).reshape(self.conf.shape)
        self.conf += conf_this
//  [python]
def add(self, output, target):
        output = output.cpu().squeeze().numpy()
        target = target.cpu().squeeze().numpy()

        if np.ndim(output) == 1:
            output = output[None]

        onehot = np.ndim(target) != 1
        assert output.shape[0] == target.shape[0], \
                'number of targets and outputs do not match'
        assert output.shape[1] == self.conf.shape[0], \
                'number of outputs does not match size of confusion matrix'
        assert not onehot or target.shape[1] == output.shape[1], \
                'target should be 1D Tensor or have size of output (one-hot)'
        if onehot:
            assert (target >= 0).all() and (target <= 1).all(), \
                    'in one-hot encoding, target values should be 0 or 1'
            assert (target.sum(1) == 1).all(), \
                    'multi-label setting is not supported'

        target = target.argmax(1) if onehot else target
        pred = output.argmax(1)

        target    = target.astype(np.int32)
        pred      = pred.astype(np.int32)
        conf_this = np.bincount(target * self.conf.shape[0] + pred,minlength=np.prod(self.conf.shape))
        conf_this = conf_this.astype(self.conf.dtype).reshape(self.conf.shape)
        self.conf += conf_this
//  [python]
def __iter__(self):
        """
        Yield Realization tuples. Notice that the weight is homogeneous when
        sampling is enabled, since it is accounted for in the sampling
        procedure.
        """
        if self.num_samples:
            # random sampling of the logic tree
            probs = random((self.num_samples, len(self.bsetdict)),
                           self.seed, self.sampling_method)
            ordinal = 0
            for branches in self.root_branchset.sample(
                    probs, self.sampling_method):
                value = [br.value for br in branches]
                smlt_path_ids = [br.branch_id for br in branches]
                if self.sampling_method.startswith('early_'):
                    weight = 1. / self.num_samples  # already accounted
                elif self.sampling_method.startswith('late_'):
                    weight = n.prod([br.weight for br in branches])
                else:
                    raise NotImplementedError(self.sampling_method)
                yield Realization(value, weight, ordinal, tuple(smlt_path_ids))
                ordinal += 1
        else:  # full enumeration
            ordinal = 0
            for weight, branches in self.root_branchset.enumerate_paths():
                value = [br.value for br in branches]
                branch_ids = [branch.branch_id for branch in branches]
                yield Realization(value, weight, ordinal, tuple(branch_ids))
                ordinal += 1
//  [python]
def __iter__(self):
        """
        Yield Realization tuples. Notice that the weight is homogeneous when
        sampling is enabled, since it is accounted for in the sampling
        procedure.
        """
        if self.num_samples:
            # random sampling of the logic tree
            probs = random((self.num_samples, len(self.bsetdict)),
                           self.seed, self.sampling_method)
            ordinal = 0
            for branches in self.root_branchset.sample(
                    probs, self.sampling_method):
                value = [br.value for br in branches]
                smlt_path_ids = [br.branch_id for br in branches]
                if self.sampling_method.startswith('early_'):
                    weight = 1. / self.num_samples  # already accounted
                elif self.sampling_method.startswith('late_'):
                    weight = numpy.prod([br.weight for br in branches])
                else:
                    raise NotImplementedError(self.sampling_method)
                yield Realization(value, weight, ordinal, tuple(smlt_path_ids))
                ordinal += 1
        else:  # full enumeration
            ordinal = 0
            for weight, branches in self.root_branchset.enumerate_paths():
                value = [br.value for br in branches]
                branch_ids = [branch.branch_id for branch in branches]
                yield Realization(value, weight, ordinal, tuple(branch_ids))
                ordinal += 1
//  [python]
def calculate_virtual_reward(self):
        """Apply the virtual reward formula to account for all the different goal scores."""
        rewards = -1 * self.states.cum_rewards if self.minimize else self.states.cum_rewards
        processed_rewards = relativize(rewards)
        score_reward = processed_rewards ** self.reward_scale
        score_dist = self.states.distances ** self.distance_scale
        virt_rw = score_reward * score_dist
        dist_prob = score_dist / score_dist.sum()
        reward_prob = score_reward / score_reward.sum()
        total_entropy = numpy.prod(2 - dist_prob ** reward_prob)
        self._min_entropy = numpy.prod(2 - reward_prob ** reward_prob)
        self.efficiency = self._min_entropy / total_entropy
        self.update_states(virtual_rewards=virt_rw, processed_rewards=processed_rewards)
        if self.critic is not None:
            critic_states = self.critic.calculate(
                walkers_states=self.states,
                model_states=self.model_states,
                env_states=self.env_states,
            )
            self.states.update(other=critic_states)
            virt_rew = self.states.virtual_rewards * self.states.critic
        else:
            virt_rew = self.states.virtual_rewards
        self.states.update(virtual_rewards=virt_rew)
//  [python]
def factorial(n):
    return n
//  [python]
def factorial(n):
    return np.prod(range(1,n+1))
//  [python]
def randomize_weights(self, scale_w = 3.0):
        fan_in = 1.0*n.prod(self.filter_shape[1:])
        # each unit in the lower layer receives a gradient from:
        # "num output feature maps * filter height * filter width * filter depth" / pooling size**3
        fan_out = 1.0*(n.prod(self.filter_shape[0:2]) * n.prod(self.filter_shape[3:])/np.mean(self.pooling_factor)**3)
        # initialize weights with random weights
        W_bound = numpy.sqrt(scale_w / (fan_in + fan_out))

        self.W.set_value(numpy.asarray(numpy.random.normal(0, W_bound, self.filter_shape), dtype=theano.config.floatX))
        self.b.set_value(numpy.asarray(numpy.zeros((self.filter_shape[0],), dtype=theano.config.floatX)))
//  [python]
def randomize_weights(self, scale_w = 3.0):
        fan_in = 1.0*numpy.prod(self.filter_shape[1:])
        # each unit in the lower layer receives a gradient from:
        # "num output feature maps * filter height * filter width * filter depth" / pooling size**3
        fan_out = 1.0*(numpy.prod(self.filter_shape[0:2]) * numpy.prod(self.filter_shape[3:])/np.mean(self.pooling_factor)**3)
        # initialize weights with random weights
        W_bound = numpy.sqrt(scale_w / (fan_in + fan_out))

        self.W.set_value(numpy.asarray(numpy.random.normal(0, W_bound, self.filter_shape), dtype=theano.config.floatX))
        self.b.set_value(numpy.asarray(numpy.zeros((self.filter_shape[0],), dtype=theano.config.floatX)))
//  [python]
def combine_fragments_to_dense_bxcyz(self, tensor, sh):
      """ expected shape: (batch, x, channels, y, z)"""
      ttensor = tensor # be same shape as result, no significant time cost

      output_stride = self.output_stride
      if isinstance(output_stride, list) or isinstance(output_stride, tuple):
          example_stride = n.prod(output_stride)#**3
      else:
          example_stride = output_stride**3
          output_stride  = np.asarray((output_stride,)*3)
      zero = np.array((0), dtype=theano.config.floatX)
      embedding = T.alloc( zero, 1, sh[1]*output_stride[0], sh[2], sh[3]*output_stride[1], sh[4]*output_stride[2]) # first arg. is fill-value (0 in this case) and not an element of the shape
      ix = offset_map(output_stride)
      print("      output_stride",output_stride)
      print("      example_stride",example_stride)
      for i,(n,m,k) in enumerate(ix):
          embedding = T.set_subtensor(embedding[:,n::output_stride[0],:,m::output_stride[1],k::output_stride[2]], ttensor[i::example_stride])
      return embedding
//  [python]
def combine_fragments_to_dense_bxcyz(self, tensor, sh):
      """ expected shape: (batch, x, channels, y, z)"""
      ttensor = tensor # be same shape as result, no significant time cost

      output_stride = self.output_stride
      if isinstance(output_stride, list) or isinstance(output_stride, tuple):
          example_stride = np.prod(output_stride)#**3
      else:
          example_stride = output_stride**3
          output_stride  = np.asarray((output_stride,)*3)
      zero = np.array((0), dtype=theano.config.floatX)
      embedding = T.alloc( zero, 1, sh[1]*output_stride[0], sh[2], sh[3]*output_stride[1], sh[4]*output_stride[2]) # first arg. is fill-value (0 in this case) and not an element of the shape
      ix = offset_map(output_stride)
      print("      output_stride",output_stride)
      print("      example_stride",example_stride)
      for i,(n,m,k) in enumerate(ix):
          embedding = T.set_subtensor(embedding[:,n::output_stride[0],:,m::output_stride[1],k::output_stride[2]], ttensor[i::example_stride])
      return embedding
//  [python]
def tune():
    with open('convolution.cl', 'r') as f:
        kernel_string = f.read()

    #setup tunable parameters
    tune_params = OrderedDict()
    tune_params["filter_height"] = [i for i in range(3,19,2)]
    tune_params["filter_width"] = [i for i in range(3,19,2)]
    tune_params["block_size_x"] = [16*i for i in range(1,65)]
    tune_params["block_size_y"] = [2**i for i in range(6)]
    tune_params["tile_size_x"] = [i for i in range(1,11)]
    tune_params["tile_size_y"] = [i for i in range(1,11)]

    tune_params["use_padding"] = [0,1]  #toggle the insertion of padding in shared memory
    tune_params["read_only"] = [0,1]    #toggle using the read-only cache

    #limit the search to only use padding when its effective, and at least 32 threads in a block
    restrict = ["use_padding==0 or (block_size_x % 32 != 0)", "block_size_x*block_size_y >= 32"]

    #setup input and output dimensions
    problem_size = (4096, 4096)
    size = n.prod(problem_size)
    largest_fh = max(tune_params["filter_height"])
    largest_fw = max(tune_params["filter_width"])
    input_size = ((problem_size[0]+largest_fw-1) * (problem_size[1]+largest_fh-1))

    #create input data
    output_image = numpy.zeros(size).astype(numpy.float32)
    input_image = numpy.random.randn(input_size).astype(numpy.float32)
    filter_weights = numpy.random.randn(largest_fh * largest_fw).astype(numpy.float32)

    #setup kernel arguments
    args = [output_image, input_image, filter_weights]

    #tell the Kernel Tuner how to compute grid dimensions
    grid_div_x = ["block_size_x", "tile_size_x"]
    grid_div_y = ["block_size_y", "tile_size_y"]

    #start tuning separable convolution (row)
    tune_params["filter_height"] = [1]
    results_row = tune_kernel("convolution_kernel", kernel_string,
        problem_size, args, tune_params,
        grid_div_y=grid_div_y, grid_div_x=grid_div_x, verbose=True, restrictions=restrict)

    #start tuning separable convolution (col)
    tune_params["filter_height"] = tune_params["filter_width"][:]
    tune_params["filter_width"] = [1]
    results_col = tune_kernel("convolution_kernel", kernel_string,
        problem_size, args, tune_params,
        grid_div_y=grid_div_y, grid_div_x=grid_div_x, verbose=True, restrictions=restrict)

    return results_row, results_col
//  [python]
def tune():
    with open('convolution.cl', 'r') as f:
        kernel_string = f.read()

    #setup tunable parameters
    tune_params = OrderedDict()
    tune_params["filter_height"] = [i for i in range(3,19,2)]
    tune_params["filter_width"] = [i for i in range(3,19,2)]
    tune_params["block_size_x"] = [16*i for i in range(1,65)]
    tune_params["block_size_y"] = [2**i for i in range(6)]
    tune_params["tile_size_x"] = [i for i in range(1,11)]
    tune_params["tile_size_y"] = [i for i in range(1,11)]

    tune_params["use_padding"] = [0,1]  #toggle the insertion of padding in shared memory
    tune_params["read_only"] = [0,1]    #toggle using the read-only cache

    #limit the search to only use padding when its effective, and at least 32 threads in a block
    restrict = ["use_padding==0 or (block_size_x % 32 != 0)", "block_size_x*block_size_y >= 32"]

    #setup input and output dimensions
    problem_size = (4096, 4096)
    size = numpy.prod(problem_size)
    largest_fh = max(tune_params["filter_height"])
    largest_fw = max(tune_params["filter_width"])
    input_size = ((problem_size[0]+largest_fw-1) * (problem_size[1]+largest_fh-1))

    #create input data
    output_image = numpy.zeros(size).astype(numpy.float32)
    input_image = numpy.random.randn(input_size).astype(numpy.float32)
    filter_weights = numpy.random.randn(largest_fh * largest_fw).astype(numpy.float32)

    #setup kernel arguments
    args = [output_image, input_image, filter_weights]

    #tell the Kernel Tuner how to compute grid dimensions
    grid_div_x = ["block_size_x", "tile_size_x"]
    grid_div_y = ["block_size_y", "tile_size_y"]

    #start tuning separable convolution (row)
    tune_params["filter_height"] = [1]
    results_row = tune_kernel("convolution_kernel", kernel_string,
        problem_size, args, tune_params,
        grid_div_y=grid_div_y, grid_div_x=grid_div_x, verbose=True, restrictions=restrict)

    #start tuning separable convolution (col)
    tune_params["filter_height"] = tune_params["filter_width"][:]
    tune_params["filter_width"] = [1]
    results_col = tune_kernel("convolution_kernel", kernel_string,
        problem_size, args, tune_params,
        grid_div_y=grid_div_y, grid_div_x=grid_div_x, verbose=True, restrictions=restrict)

    return results_row, results_col
//  [python]
def test_accelerate_framework_sgemv_fix(self):
        if sys.platform != 'darwin':
            return

        def aligned_array(shape, align, dtype, order='C'):
            d = dtype()
            N = np.prod(shape)
            tmp = np.zeros(N * d.nbytes + align, dtype=np.uint8)
            address = tmp.__array_interface__["data"][0]
            for offset in range(align):
                if (address + offset) % align == 0:
                    break
            tmp = tmp[offset:offset+N*d.nbytes].view(dtype=dtype)
            return tmp.reshape(shape, order=order)

        def as_aligned(arr, align, dtype, order='C'):
            aligned = aligned_array(arr.shape, align, dtype, order)
            aligned[:] = arr[:]
            return aligned

        def assert_dot_close(A, X, desired):
            assert_allclose(np.dot(A, X), desired, rtol=1e-5, atol=1e-7)

        m = aligned_array(100, 15, np.float32)
        s = aligned_array((100, 100), 15, np.float32)
        np.dot(s, m) # this will always segfault if the bug is present

        testdata = itertools.product((15,32), (10000,), (200,89), ('C','F'))
        for align, m, n, a_order in testdata:
            # Calculation in double precision
            A_d = np.random.rand(m, n)
            X_d = np.random.rand(n)
            desired = np.dot(A_d, X_d)
            # Calculation with aligned single precision
            A_f = as_aligned(A_d, align, np.float32, order=a_order)
            X_f = as_aligned(X_d, align, np.float32)
            assert_dot_close(A_f, X_f, desired)
            # Strided A rows
            A_d_2 = A_d[::2]
            desired = np.dot(A_d_2, X_d)
            A_f_2 = A_f[::2]
            assert_dot_close(A_f_2, X_f, desired)
            # Strided A columns, strided X vector
            A_d_22 = A_d_2[:, ::2]
            X_d_2 = X_d[::2]
            desired = np.dot(A_d_22, X_d_2)
            A_f_22 = A_f_2[:, ::2]
            X_f_2 = X_f[::2]
            assert_dot_close(A_f_22, X_f_2, desired)
            # Check the strides are as expected
            if a_order == 'F':
                assert_equal(A_f_22.strides, (8, 8 * m))
            else:
                assert_equal(A_f_22.strides, (8 * n, 8))
            assert_equal(X_f_2.strides, (8,))
            # Strides in A rows + cols only
            X_f_2c = as_aligned(X_f_2, align, np.float32)
            assert_dot_close(A_f_22, X_f_2c, desired)
            # Strides just in A cols
            A_d_12 = A_d[:, ::2]
            desired = np.dot(A_d_12, X_d_2)
            A_f_12 = A_f[:, ::2]
            assert_dot_close(A_f_12, X_f_2c, desired)
            # Strides in A cols and X
            assert_dot_close(A_f_12, X_f_2, desired)
//  [python]
def _isEmpty2d(arr):
  # check size first for efficiency
  return arr.size == 0 and arr.shape[-1] * arr.shape[-2] == 0
//  [python]
def _isEmpty2d(arr):
  # check size first for efficiency
  return arr.size == 0 and np.prod(arr.shape[-2:]) == 0
//  [python]
def flat_dim(self):
        return int(np.prod(self.shape))
//  [python]
def conv_layer(
        cls,
        name: str,
        in_channels: int,
        out_channels: int,
        kernel_shape: List[int],
        bias: bool,
        groups: int = 1,
        stride: Union[int, List[int]] = 1,
        padding: List[int] = None,
        **kwargs,
    ) -> "LayerInfo":
        """
        creates a LayerInfo object for a fully connected convolutional layer

        :param name: unique name of the layer within its model
        :param in_channels: number of input channels
        :param out_channels: number of output channels
        :param kernel_shape: kernel shape of this layer, given as a list
        :param bias: True if the linear layer has a bias add included, False otherwise
        :param groups: number of groups that input and output channels are divided into.
            default is 1
        :param stride: stride for this convolution, can be int or tuple of ints. default
            is 1
        :param padding: padding applied to each spatial axis. defualt is [0, 0, 0, 0]
        :param kwargs: additional kwargs to be passed to the LayerInfo constructor
        :return:
        """
        attributes = {
            "in_channels": in_channels,
            "out_channels": out_channels,
            "kernel_shape": kernel_shape,
            "groups": groups,
            "stride": stride,
            "padding": padding if padding is not None else [0, 0, 0, 0],
        }
        attributes.update(kwargs.get("attributes", {}))
        kwargs["attributes"] = attributes

        return cls(
            name=name,
            op_type="conv",
            params=in_channels * out_channels * n.prod(kernel_shape) // groups,
            bias_params=out_channels if bias else None,
            prunable=True,
            **kwargs,  # TODO: add FLOPS calculation
        )
//  [python]
def conv_layer(
        cls,
        name: str,
        in_channels: int,
        out_channels: int,
        kernel_shape: List[int],
        bias: bool,
        groups: int = 1,
        stride: Union[int, List[int]] = 1,
        padding: List[int] = None,
        **kwargs,
    ) -> "LayerInfo":
        """
        creates a LayerInfo object for a fully connected convolutional layer

        :param name: unique name of the layer within its model
        :param in_channels: number of input channels
        :param out_channels: number of output channels
        :param kernel_shape: kernel shape of this layer, given as a list
        :param bias: True if the linear layer has a bias add included, False otherwise
        :param groups: number of groups that input and output channels are divided into.
            default is 1
        :param stride: stride for this convolution, can be int or tuple of ints. default
            is 1
        :param padding: padding applied to each spatial axis. defualt is [0, 0, 0, 0]
        :param kwargs: additional kwargs to be passed to the LayerInfo constructor
        :return:
        """
        attributes = {
            "in_channels": in_channels,
            "out_channels": out_channels,
            "kernel_shape": kernel_shape,
            "groups": groups,
            "stride": stride,
            "padding": padding if padding is not None else [0, 0, 0, 0],
        }
        attributes.update(kwargs.get("attributes", {}))
        kwargs["attributes"] = attributes

        return cls(
            name=name,
            op_type="conv",
            params=in_channels * out_channels * numpy.prod(kernel_shape) // groups,
            bias_params=out_channels if bias else None,
            prunable=True,
            **kwargs,  # TODO: add FLOPS calculation
        )
//  [python]
def _pdf(self, **kwargs):
        """Returns the pdf at the given values. The keyword arguments must
        contain all of parameters in self's params. Unrecognized arguments are
        ignored.
        """
        for p in self._params:
            if p not in kwargs.keys():
                raise ValueError(
                    'Missing parameter {} to construct pdf.'.format(p))
        if kwargs in self:
            pdf = self._norm * \
                n.prod([(1.+kwargs[p])**(2./5)/kwargs[p]**(6./5)
                            for p in self._params])
            return float(pdf)
        else:
            return 0.0
//  [python]
def _pdf(self, **kwargs):
        """Returns the pdf at the given values. The keyword arguments must
        contain all of parameters in self's params. Unrecognized arguments are
        ignored.
        """
        for p in self._params:
            if p not in kwargs.keys():
                raise ValueError(
                    'Missing parameter {} to construct pdf.'.format(p))
        if kwargs in self:
            pdf = self._norm * \
                numpy.prod([(1.+kwargs[p])**(2./5)/kwargs[p]**(6./5)
                            for p in self._params])
            return float(pdf)
        else:
            return 0.0
//  [python]
a=[]
for _ in range(n):
	a.append([int(e) for e in raw_input().split()])
a=numpy.array(a)
a=numpy.sum(a, axis = 0)
print(numpy.prod(a, axis = None))
//  [python]
def sparse_tensor_to_dense(sparse_tensor: SparseTensorProto) -> TensorProto:
    """
    :param sparse_tensor: SparseTensorProto object
    :return: TensorProto object that is the dense representation of the given
        sparse tensor.
    """
    _check_sparse_tensor_import()
    name = sparse_tensor.values.name
    values = numpy_helper.to_array(sparse_tensor.values)
    indices = numpy_helper.to_array(sparse_tensor.indices)
    shape = sparse_tensor.dims

    dense_array = numpy.zeros(numpy.prod(shape)).astype(values.dtype)
    dense_array[indices] = values
    dense_array = dense_array.reshape(shape)

    return numpy_helper.from_array(dense_array, name=name)
//  [python]
def tune():

    #setup problem dimensions
    problem_size = (4096, 4096)
    size = n.prod(problem_size)
    input_size = (problem_size[0]+16) * (problem_size[0]+16)

    #create input and output data using pinned memory
    output = allocate(size)
    numpy.copyto(output, numpy.zeros(size).astype(numpy.float32, order='C'))
    input = allocate(input_size)
    numpy.copyto(input, numpy.random.randn(input_size).astype(numpy.float32, order='C'))
    filter = allocate(17*17)
    numpy.copyto(filter, numpy.random.randn(17*17).astype(numpy.float32, order='C'))

    #kernel arguments
    args = [output, input, filter]

    #setup tuning parameters
    tune_params = OrderedDict()
    tune_params["block_size_x"] = [16*i for i in range(1,17)]
    tune_params["block_size_y"] = [2**i for i in range(5)]
    tune_params["tile_size_x"] = [2**i for i in range(4)]
    tune_params["tile_size_y"] = [2**i for i in range(4)]
    tune_params["num_streams"] = [2**i for i in range(6)]

    #tell the Kernel Tuner how to compute grid dimensions
    grid_div_x = ["block_size_x", "tile_size_x"]
    grid_div_y = ["block_size_y", "tile_size_y", "num_streams"]

    #filter kernels that use too much shared memory
    restrict = ["(block_size_x*tile_size_x+16)*(block_size_y*tile_size_y+16) < 12*1024"]

    #compute the answer using a naive kernel
    with open('convolution.cu', 'r') as f:
        kernel_string = f.read()
    params = { "block_size_x": 16, "block_size_y": 16 }
    results = kernel_tuner.run_kernel("convolution_naive", kernel_string,
        problem_size, args, params,
        grid_div_y=["block_size_y"], grid_div_x=["block_size_x"])

    #set non-output fields to None
    answer = [results[0], None, None]

    #start tuning the kernel
    result = kernel_tuner.tune_kernel("convolution_streams", ['convolution_streams.cu', 'convolution.cu'],
        problem_size, args, tune_params,
        grid_div_y=grid_div_y, grid_div_x=grid_div_x, restrictions=restrict, answer=answer, verbose=True, lang="C",
        compiler_options=["-arch=sm_52"])

    return result
//  [python]
def tune():

    #setup problem dimensions
    problem_size = (4096, 4096)
    size = numpy.prod(problem_size)
    input_size = (problem_size[0]+16) * (problem_size[0]+16)

    #create input and output data using pinned memory
    output = allocate(size)
    numpy.copyto(output, numpy.zeros(size).astype(numpy.float32, order='C'))
    input = allocate(input_size)
    numpy.copyto(input, numpy.random.randn(input_size).astype(numpy.float32, order='C'))
    filter = allocate(17*17)
    numpy.copyto(filter, numpy.random.randn(17*17).astype(numpy.float32, order='C'))

    #kernel arguments
    args = [output, input, filter]

    #setup tuning parameters
    tune_params = OrderedDict()
    tune_params["block_size_x"] = [16*i for i in range(1,17)]
    tune_params["block_size_y"] = [2**i for i in range(5)]
    tune_params["tile_size_x"] = [2**i for i in range(4)]
    tune_params["tile_size_y"] = [2**i for i in range(4)]
    tune_params["num_streams"] = [2**i for i in range(6)]

    #tell the Kernel Tuner how to compute grid dimensions
    grid_div_x = ["block_size_x", "tile_size_x"]
    grid_div_y = ["block_size_y", "tile_size_y", "num_streams"]

    #filter kernels that use too much shared memory
    restrict = ["(block_size_x*tile_size_x+16)*(block_size_y*tile_size_y+16) < 12*1024"]

    #compute the answer using a naive kernel
    with open('convolution.cu', 'r') as f:
        kernel_string = f.read()
    params = { "block_size_x": 16, "block_size_y": 16 }
    results = kernel_tuner.run_kernel("convolution_naive", kernel_string,
        problem_size, args, params,
        grid_div_y=["block_size_y"], grid_div_x=["block_size_x"])

    #set non-output fields to None
    answer = [results[0], None, None]

    #start tuning the kernel
    result = kernel_tuner.tune_kernel("convolution_streams", ['convolution_streams.cu', 'convolution.cu'],
        problem_size, args, tune_params,
        grid_div_y=grid_div_y, grid_div_x=grid_div_x, restrictions=restrict, answer=answer, verbose=True, lang="C",
        compiler_options=["-arch=sm_52"])

    return result
//  [python]
def __init__(self,  input, filter_shape, image_shape, poolsize, non_linear, W, B):
        """
        Allocate a LeNetConvPoolLayer with shared variable internal parameters.

        :type rng: numpy.random.RandomState
        :param rng: a random number generator used to initialize weights

        :type input: theano.tensor.dtensor4
        :param input: symbolic image tensor, of shape image_shape

        :type filter_shape: tuple or list of length 4
        :param filter_shape: (number of filters, num input feature maps,
                              filter height,filter width)

        :type image_shape: tuple or list of length 4
        :param image_shape: (batch size, num input feature maps,
                             image height, image width)

        :type poolsize: tuple or list of length 2
        :param poolsize: the downsampling (pooling) factor (#rows,#cols)
        """

        assert image_shape[1] == filter_shape[1]
        self.input = input
        self.filter_shape = filter_shape
        self.image_shape = image_shape
        self.poolsize = poolsize
        self.non_linear = non_linear
        # there are "num input feature maps * filter height * filter width"
        # inputs to each hidden unit
        fan_in = n.prod(filter_shape[1:])
        # each unit in the lower layer receives a gradient from:
        # "num output feature maps * filter height * filter width" /
        #   pooling size
        fan_out = (filter_shape[0] * n.prod(filter_shape[2:]) /n.prod(poolsize))
        # initialize weights with random weights
        self.W = theano.shared(W, borrow=True,name="W_conv")
        self.b = theano.shared(B, borrow=True, name="b_conv")
        # print(W.shape)
        # print(B.shape)
        
        # convolve input feature maps with filters
        conv_out = conv.conv2d(input=input, filters=self.W,filter_shape=self.filter_shape, image_shape=self.image_shape)
        if self.non_linear=="tanh":
            conv_out_tanh = T.tanh(conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
            self.output = pool.pool_2d(input=conv_out_tanh, ds=self.poolsize, ignore_border=True)
        elif self.non_linear=="relu":
            conv_out_tanh = ReLU(conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
            self.output = pool.pool_2d(input=conv_out_tanh, ds=self.poolsize, ignore_border=True)
        else:
            pooled_out = pool.pool_2d(input=conv_out, ds=self.poolsize, ignore_border=True)
            self.output = pooled_out + self.b.dimshuffle('x', 0, 'x', 'x')
        self.params = [self.W, self.b]
//  [python]
def __init__(self,  input, filter_shape, image_shape, poolsize, non_linear, W, B):
        """
        Allocate a LeNetConvPoolLayer with shared variable internal parameters.

        :type rng: numpy.random.RandomState
        :param rng: a random number generator used to initialize weights

        :type input: theano.tensor.dtensor4
        :param input: symbolic image tensor, of shape image_shape

        :type filter_shape: tuple or list of length 4
        :param filter_shape: (number of filters, num input feature maps,
                              filter height,filter width)

        :type image_shape: tuple or list of length 4
        :param image_shape: (batch size, num input feature maps,
                             image height, image width)

        :type poolsize: tuple or list of length 2
        :param poolsize: the downsampling (pooling) factor (#rows,#cols)
        """

        assert image_shape[1] == filter_shape[1]
        self.input = input
        self.filter_shape = filter_shape
        self.image_shape = image_shape
        self.poolsize = poolsize
        self.non_linear = non_linear
        # there are "num input feature maps * filter height * filter width"
        # inputs to each hidden unit
        fan_in = numpy.prod(filter_shape[1:])
        # each unit in the lower layer receives a gradient from:
        # "num output feature maps * filter height * filter width" /
        #   pooling size
        fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) /numpy.prod(poolsize))
        # initialize weights with random weights
        self.W = theano.shared(W, borrow=True,name="W_conv")
        self.b = theano.shared(B, borrow=True, name="b_conv")
        # print(W.shape)
        # print(B.shape)
        
        # convolve input feature maps with filters
        conv_out = conv.conv2d(input=input, filters=self.W,filter_shape=self.filter_shape, image_shape=self.image_shape)
        if self.non_linear=="tanh":
            conv_out_tanh = T.tanh(conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
            self.output = pool.pool_2d(input=conv_out_tanh, ds=self.poolsize, ignore_border=True)
        elif self.non_linear=="relu":
            conv_out_tanh = ReLU(conv_out + self.b.dimshuffle('x', 0, 'x', 'x'))
            self.output = pool.pool_2d(input=conv_out_tanh, ds=self.poolsize, ignore_border=True)
        else:
            pooled_out = pool.pool_2d(input=conv_out, ds=self.poolsize, ignore_border=True)
            self.output = pooled_out + self.b.dimshuffle('x', 0, 'x', 'x')
        self.params = [self.W, self.b]
//  [python]
def default(self, obj):
        """
        if input object is a ndarray it will be converted into a dict holding dtype, shape and the data base64 encoded
        """
        if isinstance(obj, np.ndarray):
            if np.prod(obj.shape) <= 16 and obj.dtype == np.float64:
                return dict(__ndarray__=obj.tolist())
            else:
                data_b64 = str(base64.b64encode(obj.data))
                return dict(__ndarray__=data_b64,
                            dtype=str(obj.dtype),
                            shape=obj.shape)
        return json.JSONEncoder.default(self, obj)
//  [python]
def _mom(self, key, left, right, cache):
        """
        Statistical moments.

        Example:
            >>> chaospy.Uniform().mom([0, 1, 2, 3]).round(4)
            array([1.    , 0.5   , 0.3333, 0.25  ])
            >>> Multiply(chaospy.Uniform(), 2).mom([0, 1, 2, 3]).round(4)
            array([1.    , 1.    , 1.3333, 2.    ])
            >>> Multiply(2, chaospy.Uniform()).mom([0, 1, 2, 3]).round(4)
            array([1.    , 1.    , 1.3333, 2.    ])
            >>> Multiply(chaospy.Uniform(), chaospy.Uniform()).mom([0, 1, 2, 3]).round(4)
            array([1.    , 0.25  , 0.1111, 0.0625])

        """
        del cache
        if isinstance(left, Distribution):
            if chaospy.shares_dependencies(left, right):
                raise chaospy.StochasticallyDependentError(
                    "product of dependent distributions not feasible: "
                    "{} and {}".format(left, right)
                )
            left = left._get_mom(key)
        else:
            left = (numpy.array(left).T ** key).T
        if isinstance(right, Distribution):
            right = right._get_mom(key)
        else:
            right = (numpy.array(right).T ** key).T
        return numpy.prod(left) * numpy.prod(right)
//  [python]
def _get_nan_decs(self):
        """Set all variables to nan for specializations of fused types for
        which don't have signatures.

        """
        # Set non fused-type variables to nan
        tab = " "*4
        fused_types, lines = [], [tab + "else:"]
        seen = set()
        for outvar, outtype, code in zip(self.outvars, self.outtypes,
                                         self.outcodes):
            if len(code) == 1:
                line = f"{outvar}[0] = {NAN_VALUE[code]}"
                lines.append(2*tab + line)
            else:
                fused_type = outtype
                name, _ = fused_type
                if name not in seen:
                    fused_types.append(fused_type)
                    seen.add(name)
        if not fused_types:
            return lines

        # Set fused-type variables to nan
        all_codes = tuple([codes for _unused, codes in fused_types])

        codelens = [len(x) for x in all_codes]
        last = n.prod(codelens) - 1
        for m, codes in enumerate(itertools.product(*all_codes)):
            fused_codes, decs = [], []
            for n, fused_type in enumerate(fused_types):
                code = codes[n]
                fused_codes.append(underscore(CY_TYPES[code]))
                for nn, outvar in enumerate(self.outvars):
                    if self.outtypes[nn] == fused_type:
                        line = f"{outvar}[0] = {NAN_VALUE[code]}"
                        decs.append(line)
            if m == 0:
                adverb = "if"
            elif m == last:
                adverb = "else"
            else:
                adverb = "elif"
            cond = self._get_conditional(fused_types, codes, adverb)
            lines.append(2*tab + cond)
            lines.extend([3*tab + x for x in decs])
        return lines
//  [python]
def _get_nan_decs(self):
        """Set all variables to nan for specializations of fused types for
        which don't have signatures.

        """
        # Set non fused-type variables to nan
        tab = " "*4
        fused_types, lines = [], [tab + "else:"]
        seen = set()
        for outvar, outtype, code in zip(self.outvars, self.outtypes,
                                         self.outcodes):
            if len(code) == 1:
                line = f"{outvar}[0] = {NAN_VALUE[code]}"
                lines.append(2*tab + line)
            else:
                fused_type = outtype
                name, _ = fused_type
                if name not in seen:
                    fused_types.append(fused_type)
                    seen.add(name)
        if not fused_types:
            return lines

        # Set fused-type variables to nan
        all_codes = tuple([codes for _unused, codes in fused_types])

        codelens = [len(x) for x in all_codes]
        last = numpy.prod(codelens) - 1
        for m, codes in enumerate(itertools.product(*all_codes)):
            fused_codes, decs = [], []
            for n, fused_type in enumerate(fused_types):
                code = codes[n]
                fused_codes.append(underscore(CY_TYPES[code]))
                for nn, outvar in enumerate(self.outvars):
                    if self.outtypes[nn] == fused_type:
                        line = f"{outvar}[0] = {NAN_VALUE[code]}"
                        decs.append(line)
            if m == 0:
                adverb = "if"
            elif m == last:
                adverb = "else"
            else:
                adverb = "elif"
            cond = self._get_conditional(fused_types, codes, adverb)
            lines.append(2*tab + cond)
            lines.extend([3*tab + x for x in decs])
        return lines
//  [python]
def num_elements(tensor: T.Tensor) -> int:
    """
    Return the number of elements in a tensor.

    Args:
        tensor: A tensor:

    Returns:
        int: The number of elements in the tensor.

    """
    return n.prod(shape(tensor))
//  [python]
def num_elements(tensor: T.Tensor) -> int:
    """
    Return the number of elements in a tensor.

    Args:
        tensor: A tensor:

    Returns:
        int: The number of elements in the tensor.

    """
    return numpy.prod(shape(tensor))
//  [python]
c = c.astype(dtype)

alpha = 1.1
beta = 1.0

perf = cupyx.time.repeat(
    cutensor.contraction,
    (alpha, a, mode_a, b, mode_b, beta, c, mode_c),
    n_warmup=1, n_repeat=5)

total_flops = 2 * numpy.prod(numpy.array(list(extent.values())))
//  [python]
def tprod(x: T.Tensor, axis: int=None, keepdims: bool=False) -> T.FloatingPoint:
    """
    Return the product of the elements of a tensor along the specified axis.

    Args:
        x: A float or tensor.
        axis (optional): The axis for taking the product.
        keepdims (optional): If this is set to true, the dimension of the tensor
                             is unchanged. Otherwise, the reduced axis is removed
                             and the dimension of the array is 1 less.

    Returns:
        if axis is None:
            float: The overall product of the elements in the tensor
        else:
            tensor: The product of the tensor along the specified axis.

    """
    return n.prod(x, axis=axis, keepdims=keepdims)
//  [python]
def tprod(x: T.Tensor, axis: int=None, keepdims: bool=False) -> T.FloatingPoint:
    """
    Return the product of the elements of a tensor along the specified axis.

    Args:
        x: A float or tensor.
        axis (optional): The axis for taking the product.
        keepdims (optional): If this is set to true, the dimension of the tensor
                             is unchanged. Otherwise, the reduced axis is removed
                             and the dimension of the array is 1 less.

    Returns:
        if axis is None:
            float: The overall product of the elements in the tensor
        else:
            tensor: The product of the tensor along the specified axis.

    """
    return numpy.prod(x, axis=axis, keepdims=keepdims)
//  [python]
def conv2d_forward_batch(self, inputs, weights, bias, outputs,
                             padding, stride):
        num_filters = weights.shape[0]
        num_images, input_rows, input_cols, num_input_maps = inputs.shape
        kernel_shape = weights.shape[1:]
        num_output_pixels = outputs.shape[1] * outputs.shape[2]
        num_kernel_params = np.multiply.reduce(kernel_shape)
        out_shape = (num_output_pixels, num_filters)

        for i in range(num_images):
            col = np.zeros((num_output_pixels, num_kernel_params),
                           dtype=self.dtype)
            brainstorm.handlers._cpuop.im2col(
                inputs[i].reshape(inputs[i].size),
                input_rows, input_cols, num_input_maps,
                kernel_shape[0], kernel_shape[1],
                padding, padding, padding, padding, stride[0],
                stride[1], col.reshape(col.size))

            reshaped_params = weights.reshape(num_filters, num_kernel_params)
            np.dot(col, reshaped_params.T, out=outputs[i].reshape(out_shape))

        outputs += bias.reshape((1, 1, 1, num_filters))