Like `torch.as_tensor`, but handle lists too, and can pass multiple vector elements directly.
168926
def tensor(x:Any, *rest)->Tensor:
    ""
    if len(rest): x = (x,)+rest
    # XXX: Pytorch bug in dataloader using num_workers>0; TODO: create repro and report
    if is_listy(x) and len(x)==0: return tensor(0)
    res = torch.tensor(x) if is_listy(x) else as_tensor(x)
    if res.dtype is torch.int32:
        warn('Tensor is int32: upgrading to int64; for better performance use int64 input')
        return res.long()
    return res
576391
def split(attrs, inputs, proto_obj):
    """"""
    split_list = attrs.get('split') if 'split' in attrs else []
    new_attrs = translation_utils._fix_attribute_names(attrs,
                                                       {'split' : 'num_outputs'})
    if 'axis' not in attrs:
        new_attrs = translation_utils._add_extra_attributes(new_attrs, {'axis': 0})

    if not split_list:
        num_outputs = len(proto_obj.model_metadata.get('output_tensor_data'))
    else:
        if len(set(split_list)) == 1:
            num_outputs = len(split_list)
        else:
            raise NotImplementedError("Operator {} in MXNet does not support variable splits."
                                      "Tracking the issue to support variable split here: "
                                      "https://github.com/apache/incubator-mxnet/issues/11594"
                                      .format('split'))

    new_attrs['num_outputs'] = num_outputs
    return 'split', new_attrs, inputs
819158
def to_float(b:Collection[Tensor])->Collection[Tensor]:
    ""
    if is_listy(b): return [to_float(o) for o in b]
    return b.float() if b.dtype not in [torch.int64, torch.int32, torch.int16] else b
1365574
def tensor_vstack(tensor_list, pad=0):
    
    if len(tensor_list) == 1:
        return tensor_list[0][np.newaxis, :]

    ndim = len(tensor_list[0].shape)
    dimensions = [len(tensor_list)]  # first dim is batch size
    for dim in range(ndim):
        dimensions.append(max([tensor.shape[dim] for tensor in tensor_list]))

    dtype = tensor_list[0].dtype
    if pad == 0:
        all_tensor = np.zeros(tuple(dimensions), dtype=dtype)
    elif pad == 1:
        all_tensor = np.ones(tuple(dimensions), dtype=dtype)
    else:
        all_tensor = np.full(tuple(dimensions), pad, dtype=dtype)
    if ndim == 1:
        for ind, tensor in enumerate(tensor_list):
            all_tensor[ind, :tensor.shape[0]] = tensor
    elif ndim == 2:
        for ind, tensor in enumerate(tensor_list):
            all_tensor[ind, :tensor.shape[0], :tensor.shape[1]] = tensor
    elif ndim == 3:
        for ind, tensor in enumerate(tensor_list):
            all_tensor[ind, :tensor.shape[0], :tensor.shape[1], :tensor.shape[2]] = tensor
    else:
        raise Exception('Sorry, unimplemented.')
    return all_tensor
292576
def forward(self, x: torch.Tensor, sublayer: Callable[[torch.Tensor], torch.Tensor]) -> torch.Tensor:
        """"""
        return x + self.dropout(sublayer(self.norm(x)))
1048714
def _batch(self,
               vectors,
               batch_size,
               num,
               show_progressbar,
               return_names):
        """"""
        vectors = self.normalize(vectors)

        # Single transpose, makes things faster.
        reference_transposed = self.norm_vectors.T

        for i in tqdm(range(0, len(vectors), batch_size),
                      disable=not show_progressbar):

            distances = vectors[i: i+batch_size].dot(reference_transposed)
            # For safety we clip
            distances = np.clip(distances, a_min=.0, a_max=1.0)
            if num == 1:
                sorted_indices = np.argmax(distances, 1)[:, None]
            else:
                sorted_indices = np.argpartition(-distances, kth=num, axis=1)
                sorted_indices = sorted_indices[:, :num]
            for lidx, indices in enumerate(sorted_indices):
                dists = distances[lidx, indices]
                if return_names:
                    dindex = np.argsort(-dists)
                    yield [(self.indices[indices[d]], dists[d])
                           for d in dindex]
                else:
                    yield list(-1 * np.sort(-dists))
1324857
def astensor(array: TensorLike) -> BKTensor:
    """"""
    tensor = tf.convert_to_tensor(value=array, dtype=CTYPE)
    return tensor
321704
def encode(self, sequence):
        
        sequence = super().encode(sequence)
        sequence = self.tokenize(sequence)
        vector = [self.stoi.get(token, self.unknown_index) for token in sequence]
        if self.append_eos:
            vector.append(self.eos_index)
        return torch.tensor(vector)
268633
def np2model_tensor(a):
    ""
    dtype = model_type(a.dtype)
    res = as_tensor(a)
    if not dtype: return res
    return res.type(dtype)
355813
def evaluate(tensor: BKTensor) -> TensorLike:
    """"""
    if isinstance(tensor, _DTYPE):
        if torch.numel(tensor) == 1:
            return tensor.item()
        if tensor.numel() == 2:
            return tensor[0].cpu().numpy() + 1.0j * tensor[1].cpu().numpy()

        return tensor[0].cpu().numpy() + 1.0j * tensor[1].cpu().numpy()
    return tensor
78870
def vectors_between_pts(pts=[]):
    '''
    '''
    assert isinstance(pts, list) and len(pts) > 0
    l_pts = len(pts)
    l_pt_prev = None
    for pt in pts:
        assert isinstance(pt, tuple)
        l_pt = len(pt)
        assert l_pt > 1
        for i in pt:
            assert isinstance(i, float)
        if l_pt_prev is not None:
            assert l_pt == l_pt_prev
        l_pt_prev = l_pt

    return [tuple([pts[(i+1) % l_pts][j] - pts[i][j] for j in range(l_pt)]) \
            for i in range(l_pts)]
896683
def merge_first_two_dims(tensor):
    
    shape = tensor.shape
    batch_size = shape[0] * shape[1]
    new_shape = tuple([batch_size] + list(shape[2:]))
    return tensor.view(new_shape)
560578
def nested_shape(array_or_tuple):
    
    if hasattr(array_or_tuple, 'size'):
        # pytorch tensors use V.size() to get size of tensor
        return list(array_or_tuple.size())
    elif hasattr(array_or_tuple, 'get_shape'):
        # tensorflow uses V.get_shape() to get size of tensor
        return array_or_tuple.get_shape().as_list()
    elif hasattr(array_or_tuple, 'shape'):
        return array_or_tuple.shape

    try:
        # treat object as iterable
        return [nested_shape(item) for item in list(array_or_tuple)]
    except TypeError:
        # object is not actually iterable
        # LB: Maybe we should throw an error?
        return []
1035920
def cast_like(x, y):
  """"""
  x = tf.convert_to_tensor(x)
  y = tf.convert_to_tensor(y)

  if x.dtype.base_dtype == y.dtype.base_dtype:
    return x

  cast_x = tf.cast(x, y.dtype)
  if cast_x.device != x.device:
    x_name = "(eager Tensor)"
    try:
      x_name = x.name
    except AttributeError:
      pass
    tf.logging.warning("Cast for %s may induce copy from '%s' to '%s'", x_name,
                       x.device, cast_x.device)
  return cast_x
705044
def astensor(array: TensorLike) -> BKTensor:
    """"""
    tensor = tf.convert_to_tensor(array, dtype=CTYPE)
    if DEVICE == 'gpu':
        tensor = tensor.gpu()  # pragma: no cover

    # size = np.prod(np.array(tensor.get_shape().as_list()))
    N = int(math.log2(size(tensor)))
    tensor = tf.reshape(tensor, ([2]*N))

    return tensor
371106
def listify(p:OptListOrItem=None, q:OptListOrItem=None):
    ""
    if p is None: p=[]
    elif isinstance(p, str):          p = [p]
    elif not isinstance(p, Iterable): p = [p]
    #Rank 0 tensors in PyTorch are Iterable but don't have a length.
    else:
        try: a = len(p)
        except: p = [p]
    n = q if type(q)==int else len(p) if q is None else len(q)
    if len(p)==1: p = p * n
    assert len(p)==n, f'List len mismatch ({len(p)} vs {n})'
    return list(p)
1013408
def vector_element(r, i, j):
    

    """
    return Matrix([r[p][i, j] for p in range(3)])