in core/maxframe/tensor/arithmetic/clip.py [0:0]
def __call__(self, a, a_min, a_max, out=None):
a = astensor(a)
tensors = [a]
sparse = a.issparse()
if isinstance(a_min, Number):
if a_min > 0:
sparse = False
a_min_dtype = np.array(a_min).dtype
elif a_min is not None:
a_min = astensor(a_min)
tensors.append(a_min)
if not a_min.issparse():
sparse = False
a_min_dtype = a_min.dtype
else:
a_min_dtype = None
self.a_min = a_min
if isinstance(a_max, Number):
if a_max < 0:
sparse = False
a_max_dtype = np.array(a_max).dtype
elif a_max is not None:
a_max = astensor(a_max)
tensors.append(a_max)
if not a_max.issparse():
sparse = False
a_max_dtype = a_max.dtype
else:
a_max_dtype = None
self.a_max = a_max
if out is not None:
if isinstance(out, Tensor):
self.out = out
else:
raise TypeError(f"out should be Tensor object, got {type(out)} instead")
dtypes = [dt for dt in [a.dtype, a_min_dtype, a_max_dtype] if dt is not None]
dtype = np.result_type(*dtypes)
# check broadcast
shape = broadcast_shape(*[t.shape for t in tensors])
setattr(self, "sparse", sparse)
inputs = filter_inputs([a, a_min, a_max, out])
t = self.new_tensor(inputs, shape)
if out is None:
setattr(self, "dtype", dtype)
return t
# if `out` is specified, use out's dtype and shape
out_shape, out_dtype = out.shape, out.dtype
if t.shape != out_shape:
t = self.new_tensor(inputs, out_shape)
setattr(self, "dtype", out_dtype)
out.data = t.data
return out