Merge pull request #186 from lzhengning/doc

[Doc]: add docstrings for several ops and update docstring styles.
This commit is contained in:
Jittor 2021-03-16 18:48:24 +08:00 committed by GitHub
commit 2e0c0b8288
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 943 additions and 499 deletions

View File

@ -407,6 +407,15 @@ def sqr(x): return x*x
Var.sqr = sqr
def pow(x, y):
''' computes x^y, element-wise.
This operation is equivalent to ``x ** y``.
:param x: the first input.
:type x: a python number or jt.Var.
:param y: the second input.
:type y: a python number or jt.Var.
'''
if isinstance(y, (ori_int, ori_float)) and y == 2:
return x.sqr()
return core.ops.pow(x, y)

View File

@ -642,11 +642,11 @@ def compile_src(src, h, basename):
for did, has_return in enumerate(arr_has_return):
df = dfs[did]
func_call = arr_func_call[did]
if df["doc"]:
if df["doc"] and not (did > 0 and df["doc"] == dfs[did - 1]["doc"]):
doc_all += "Document:\n"
doc_all += df["doc"]
doc_all += "\nDeclaration:\n"
doc_all += df["dec"]
doc_all += df["doc"]+'\n'
doc_all += "Declaration:\n"
doc_all += df["dec"]+'\n\n'
decs += " " + df["dec"]+'\n'
if has_return:
assert "-> int" not in func_head

View File

@ -18,6 +18,32 @@ struct ArgReduceOp : Op {
NanoString op;
int dim;
bool keepdims;
/**
Returns the indices of the maximum / minimum of the input across a dimension.
----------------
* [in] x: the input jt.Var.
* [in] op: "max" or "min".
* [in] dim: int. Specifies which dimension to be reduced.
* [in] keepdim: bool. Whether the output has ``dim`` retained or not.
----------------
Example-1::
>>> x = jt.randint(0, 10, shape=(2, 3))
>>> x
jt.Var([[4 2 5]
[6 7 1]], dtype=int32)
>>> jt.arg_reduce(x, 'max', dim=1, keepdims=False)
[jt.Var([2 1], dtype=int32), jt.Var([5 7], dtype=int32)]
>>> jt.arg_reduce(x, 'min', dim=1, keepdims=False)
[jt.Var([1 2], dtype=int32), jt.Var([5 7], dtype=int32)]
*/
// @attrs(multiple_outputs)
ArgReduceOp(Var* x, NanoString op, int dim, bool keepdims);
VarPtr grad(Var* out, Var* dout, Var* v, int v_index) override;
@ -28,4 +54,4 @@ struct ArgReduceOp : Op {
DECLARE_jit_run;
};
} // jittor
} // jittor

View File

@ -29,345 +29,391 @@ static auto make_number = get_op_info("number")
unordered_set<string> binary_ops = {
/**
* computes x^y, element-wise.
*
* This operation is equivalent to ``x ** y``.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
Computes ``x^y``, element-wise.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
*/
// @pybind(pow, __pow__)
"pow",
/**
* returns the element-wise maximum of x and y.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
Returns the element-wise maximum of ``x`` and ``y``.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
*/
"maximum",
/**
* returns the element-wise minimum of x and y.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
Returns the element-wise minimum of ``x`` and ``y``.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
*/
"minimum",
/**
* element-wise adds x and y and returns a new Var.
*
* This operation is equivalent to ``x + y``.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
Element-wise adds ``x`` and ``y`` and returns a new Var.
This operation is equivalent to ``x + y``.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
*/
// @pybind(add, __add__)
"add",
/**
* element-wise subtract y from x and returns a new Var.
*
* This operation is equivalent to ``x - y``.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
Element-wise subtract ``y`` from ``x`` and returns a new Var.
This operation is equivalent to ``x - y``.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
*/
// @pybind(subtract, __sub__)
"subtract",
/**
* element-wise muliplies x with y and returns a new Var.
*
* This operation is equivalent to ``x * y``.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
Element-wise muliplies ``x`` with ``y`` and returns a new Var.
This operation is equivalent to ``x * y``.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
*/
// @pybind(multiply, __mul__)
"multiply",
/**
* element-wise divide x by y and returns a new Var.
*
* This operation is equivalent to ``x / y``.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
*
* Example:
*
* >>> a = jt.empty((3,), dtype=jt.int32)
* >>> a
* jt.Var([707406378 707406378 707406378], dtype=int32)
* >>> b = jt.empty((3,), dtype=jt.int32)
* >>> b
* jt.Var([674510453 171649398 538976288], dtype=int32)
* >>> jt.divide(a, b)
* jt.Var([1.0487701 4.1212287 1.3125001], dtype=float32)
* >>> a / b
* jt.Var([1.0487701 4.1212287 1.3125001], dtype=float32)
*
* .. note ::
* returns float value even if the dtype of input Vars are both integers.
* @see jt.ops.floor_divide() for floor division.
Element-wise divide ``x`` by ``y`` and returns a new Var.
This operation is equivalent to ``x / y``.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
----------------
Example-1::
>>> a = jt.empty((3,), dtype=jt.int32)
>>> a
jt.Var([707406378 707406378 707406378], dtype=int32)
>>> b = jt.empty((3,), dtype=jt.int32)
>>> b
jt.Var([674510453 171649398 538976288], dtype=int32)
>>> jt.divide(a, b)
jt.Var([1.0487701 4.1212287 1.3125001], dtype=float32)
>>> a / b
jt.Var([1.0487701 4.1212287 1.3125001], dtype=float32)
.. note ::
returns float value even if the dtype of input Vars are both integers.
@see jt.ops.floor_divide() for floor division.
*/
// @pybind(divide, __truediv__)
"divide",
/**
* element-wise divide x by y and returns the floor of the result.
*
* This operation is equivalent to ``x // y``.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
*
* Example:
*
* >>> a = jt.randint(1, 10, (3,), dtype=jt.int32)
* >>> a
* jt.Var([9 2 7], dtype=int32)
* >>> b = jt.randint(1, 10, (3,), dtype=jt.int32)
* >>> b
* jt.Var([6 4 6], dtype=int32)
* >>> jt.floor_divide(a, b)
* jt.Var([1 0 1], dtype=int32)
* >>> a // b
* jt.Var([1 0 1], dtype=int32)
Element-wise divide ``x`` by ``y`` and returns the floor of the result.
This operation is equivalent to ``x // y``.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
----------------
Example-1::
>>> a = jt.randint(1, 10, (3,), dtype=jt.int32)
>>> a
jt.Var([9 2 7], dtype=int32)
>>> b = jt.randint(1, 10, (3,), dtype=jt.int32)
>>> b
jt.Var([6 4 6], dtype=int32)
>>> jt.floor_divide(a, b)
jt.Var([1 0 1], dtype=int32)
>>> a // b
jt.Var([1 0 1], dtype=int32)
*/
// @pybind(floor_divide, __floordiv__)
"floor_divide",
/**
* returns the element-wise remainder of division.
*
* This operation is equivalent to ``x % y``.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
*
* Example:
*
* >>> a = jt.rand(3)
* >>> a
* jt.Var([0.3989529 0.20159635 0.22973768], dtype=float32)
* >>> b = jt.rand(3)
* >>> b
* jt.Var([0.20121202 0.7704864 0.5654395 ], dtype=float32)
* >>> jt.mod(a, b)
* jt.Var([0.19774088 0.20159635 0.22973768], dtype=float32)
* >>> a % b
* jt.Var([0.19774088 0.20159635 0.22973768], dtype=float32)
Returns the element-wise remainder of division.
This operation is equivalent to ``x % y``.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
----------------
Example-1::
>>> a = jt.rand(3)
>>> a
jt.Var([0.3989529 0.20159635 0.22973768], dtype=float32)
>>> b = jt.rand(3)
>>> b
jt.Var([0.20121202 0.7704864 0.5654395 ], dtype=float32)
>>> jt.mod(a, b)
jt.Var([0.19774088 0.20159635 0.22973768], dtype=float32)
>>> a % b
jt.Var([0.19774088 0.20159635 0.22973768], dtype=float32)
*/
// @pybind(mod, __mod__)
"mod",
/**
* returns x < y element-wise.
*
* This operation is equivalent to ``x < y``.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
Returns ``x < y`` element-wise.
This operation is equivalent to ``x < y``.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
*/
// @pybind(less, __lt__)
"less",
/**
* returns x <= y element-wise.
*
* This operation is equivalent to ``x <= y``.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
Returns ``x <= y`` element-wise.
This operation is equivalent to ``x <= y``.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
*/
// @pybind(less_equal, __le__)
"less_equal",
/**
* returns x > y element-wise.
*
* This operation is equivalent to ``x > y``.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
Returns ``x > y`` element-wise.
This operation is equivalent to ``x > y``.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
*/
// @pybind(greater, __gt__)
"greater",
/**
* returns x >= y element-wise.
*
* This operation is equivalent to ``x >= y``.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
Returns ``x >= y`` element-wise.
This operation is equivalent to ``x >= y``.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
*/
// @pybind(greater_equal, __ge__)
"greater_equal",
/**
* returns x == y element-wise.
*
* This operation is equivalent to ``x == y``.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
Returns ``x == y`` element-wise.
This operation is equivalent to ``x == y``.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
*/
// @pybind(equal, __eq__)
"equal",
/**
* returns x != y element-wise.
*
* This operation is equivalent to ``x != y``.
*
* :param x: the first input.
* :type x: a python number or jt.Var.
* :param y: the second input.
* :type y: a python number or jt.Var.
Returns ``x != y`` element-wise.
This operation is equivalent to ``x != y``.
----------------
* [in] x: the first input, a python number or jt.Var.
* [in] y: the second input, a python number or jt.Var.
*/
// @pybind(not_equal, __ne__)
"not_equal",
/**
* shifts the bits of x to the left by y.
*
* Bits are shifted to the left by appending ``y`` 0s at the right of ``x``.
* This operation is equivalent to ``x << y``.
*
* :param x: the first input.
* :type x: a python integer, or jt.Var (int32 or int64 types).
* :param y: the second input.
* :type y: a python integer, or jt.Var (int32 or int64 types).
*
* Example:
* >>> a = jt.randint(0, 10, shape=(3,))
* >>> a
* jt.Var([7 6 7], dtype=int32)
* >>> b = jt.randint(0, 10, shape=(3,))
* >>> b
* jt.Var([3 9 8], dtype=int32)
* >>> jt.left_shift(a, b)
* jt.Var([ 56 3072 1792], dtype=int32)
* >>> a << b
* jt.Var([ 56 3072 1792], dtype=int32)
Shifts the bits of ``x`` to the left by ``y``.
Bits are shifted to the left by appending ``y`` 0s at the right of ``x``.
This operation is equivalent to ``x << y``.
----------------
* [in] x: the first input, a python number or jt.Var (int32 or int64).
* [in] y: the second input, a python number or jt.Var (int32 or int64).
----------------
Example-1::
>>> a = jt.randint(0, 10, shape=(3,))
>>> a
jt.Var([7 6 7], dtype=int32)
>>> b = jt.randint(0, 10, shape=(3,))
>>> b
jt.Var([3 9 8], dtype=int32)
>>> jt.left_shift(a, b)
jt.Var([ 56 3072 1792], dtype=int32)
>>> a << b
jt.Var([ 56 3072 1792], dtype=int32)
*/
// @pybind(left_shift, __lshift__)
"left_shift",
/**
* shifts the bits of x to the right by y.
*
* This operation is equivalent to ``x >> y``.
*
* :param x: the first input.
* :type x: a python integer, or jt.Var (int32 or int64 types).
* :param y: the second input.
* :type y: a python integer, or jt.Var (int32 or int64 types).
*
* Example:
* >>> a = jt.randint(0, 1024, shape=(3,))
* >>> a
* jt.Var([439 113 92], dtype=int32)
* >>> b = jt.randint(0, 10, shape=(3,))
* >>> b
* jt.Var([6 8 4], dtype=int32)
* >>> jt.right_shift(a, b)
* jt.Var([6 0 5], dtype=int32)
Shifts the bits of ``x`` to the right by ``y``.
This operation is equivalent to ``x >> y``.
----------------
* [in] x: the first input, a python number or jt.Var (int32 or int64).
* [in] y: the second input, a python number or jt.Var (int32 or int64).
----------------
Example-1::
>>> a = jt.randint(0, 1024, shape=(3,))
>>> a
jt.Var([439 113 92], dtype=int32)
>>> b = jt.randint(0, 10, shape=(3,))
>>> b
jt.Var([6 8 4], dtype=int32)
>>> jt.right_shift(a, b)
jt.Var([6 0 5], dtype=int32)
*/
// @pybind(right_shift, __rshift__)
"right_shift",
/**
* returns the element-wise logical AND of the inputs.
*
* :param x: the first input.
* :type x: jt.Var.
* :param y: the second input.
* :type y: jt.Var.
Returns the element-wise logical AND of the inputs.
----------------
* [in] x: the first input, jt.Var.
* [in] y: the second input, jt.Var.
*/
"logical_and",
/**
* returns the element-wise logical OR of the inputs.
*
* :param x: the first input.
* :type x: jt.Var.
* :param y: the second input.
* :type y: jt.Var.
Returns the element-wise logical OR of the inputs.
----------------
* [in] x: the first input, jt.Var.
* [in] y: the second input, jt.Var.
*/
"logical_or",
/**
* returns the element-wise logical XOR of the inputs.
*
* :param x: the first input.
* :type x: jt.Var.
* :param y: the second input.
* :type y: jt.Var.
Returns the element-wise logical XOR of the inputs.
----------------
* [in] x: the first input, jt.Var.
* [in] y: the second input, jt.Var.
*/
"logical_xor",
/**
* Computes the bitwise AND of x and y.
*
* :param x: the first input.
* :type x: jt.Var (integal or boolean types).
* :param y: the second input.
* :type y: jt.Var (integal or boolean types).
Computes the bitwise AND of x and y.
----------------
* [in] x: the first input, jt.Var (integal or boolean).
* [in] y: the second input, jt.Var (integal or boolean).
*/
// @pybind(bitwise_and, __and__)
"bitwise_and",
/**
* Computes the bitwise OR of x and y.
*
* :param x: the first input.
* :type x: jt.Var (integal or boolean types).
* :param y: the second input.
* :type y: jt.Var (integal or boolean types).
Computes the bitwise OR of x and y.
----------------
* [in] x: the first input, jt.Var (integal or boolean).
* [in] y: the second input, jt.Var (integal or boolean).
*/
// @pybind(bitwise_or, __or__)
"bitwise_or",
/**
* Computes the bitwise XOR of x and y.
*
* :param x: the first input.
* :type x: jt.Var (integal or boolean types).
* :param y: the second input.
* :type y: jt.Var (integal or boolean types).
Computes the bitwise XOR of x and y.
----------------
* [in] x: the first input, jt.Var (integal or boolean).
* [in] y: the second input, jt.Var (integal or boolean).
*/
// @pybind(bitwise_xor, __xor__)
"bitwise_xor",

View File

@ -16,8 +16,72 @@ struct BroadcastToOp : Op {
uint16 bcast_mask;
uint16 keepdims_mask;
/**
Broadcast ``x`` to a given shape.
----------------
* [in] x: the input jt.Var.
* [in] shape: the output shape.
* [in] dims: specifies the new dimension in the output shape, an integer array.
----------------
Example-1::
>>> x = jt.randint(0, 10, shape=(2, 2))
>>> x
jt.Var([[8 1]
[7 6]], dtype=int32)
>>> jt.broadcast(x, shape=(2, 3, 2), dims=[1])
jt.Var([[[8 1]
[8 1]
[8 1]],
[[7 6]
[7 6]
[7 6]]], dtype=int32)
*/
// @pybind(broadcast)
BroadcastToOp(Var* x, NanoVector shape, NanoVector dims=NanoVector());
/**
Broadcast ``x`` to the same shape as ``y``.
----------------
* [in] x: the input jt.Var.
* [in] y: the reference jt.Var.
* [in] dims: specifies the new dimension in the output shape, an integer array.
----------------
.. note::
jt.broadcast_var(x, y, dims) is an alias of jt.broadcast(x, y, dims)
Example-1::
>>> x = jt.randint(0, 10, shape=(2, 2))
>>> x
jt.Var([[8 1]
[7 6]], dtype=int32)
>>> y = jt.randint(0, 10, shape=(2, 3, 2))
>>> jt.broadcast(x, y, dims=[1])
jt.Var([[[8 1]
[8 1]
[8 1]],
[[7 6]
[7 6]
[7 6]]], dtype=int32)
>>> jt.broadcast_var(x, y, dims=[1])
jt.Var([[[8 1]
[8 1]
[8 1]],
[[7 6]
[7 6]
[7 6]]], dtype=int32)
*/
// @pybind(broadcast,broadcast_var)
BroadcastToOp(Var* x, Var* y, NanoVector dims=NanoVector());
// @pybind(None)

View File

@ -27,22 +27,224 @@ static auto make_number = get_op_info("number")
NanoString binary_dtype_infer(NanoString op, Var* dx, Var* dy);
unordered_set<string> reduce_ops = {
/**
Returns the maximum elements in the input.
----------------
* [in] x: the input jt.Var.
* [in] dim: int or tuples of ints (optional). If specified, reduce along the given the dimension(s).
* [in] keepdim: bool (optional). Whether the output has ``dim`` retained or not. Defaults to be False.
----------------
Example-1::
>>> x = jt.randint(10, shape=(2, 3))
>>> x
jt.Var([[4 1 2]
[0 2 4]], dtype=int32)
>>> jt.max(x)
jt.Var([4], dtype=int32)
>>> x.max()
jt.Var([4], dtype=int32)
>>> x.max(dim=1)
jt.Var([4 4], dtype=int32)
>>> x.max(dim=1, keepdims=True)
jt.Var([[4]
[4]], dtype=int32)
*/
// @pybind(max, reduce_maximum)
"maximum",
/**
Returns the minimum elements in the input.
----------------
* [in] x: the input jt.Var.
* [in] dim: int or tuples of ints (optional). If specified, reduce along the given the dimension(s).
* [in] keepdim: bool (optional). Whether the output has ``dim`` retained or not. Defaults to be False.
----------------
Example-1::
>>> x = jt.randint(10, shape=(2, 3))
>>> x
jt.Var([[4 1 2]
[0 2 4]], dtype=int32)
>>> jt.min(x)
jt.Var([0], dtype=int32)
>>> x.min()
jt.Var([0], dtype=int32)
>>> x.min(dim=1)
jt.Var([1 0], dtype=int32)
>>> x.min(dim=1, keepdims=True)
jt.Var([[1]
[0]], dtype=int32)
*/
// @pybind(min, reduce_minimum)
"minimum",
/**
Returns the sum of the input.
----------------
* [in] x: the input jt.Var.
* [in] dim: int or tuples of ints (optional). If specified, reduce along the given the dimension(s).
* [in] keepdim: bool (optional). Whether the output has ``dim`` retained or not. Defaults to be False.
----------------
Example-1::
>>> x = jt.randint(10, shape=(2, 3))
>>> x
jt.Var([[4 1 2]
[0 2 4]], dtype=int32)
>>> jt.sum(x)
jt.Var([13], dtype=int32)
>>> x.sum()
jt.Var([13], dtype=int32)
>>> x.sum(dim=1)
jt.Var([7 6], dtype=int32)
>>> x.sum(dim=1, keepdims=True)
jt.Var([[7]
[6]], dtype=int32)
*/
// @pybind(sum, reduce_add)
"add",
/**
Returns the product of all the elements in the input.
----------------
* [in] x: the input jt.Var.
* [in] dim: int or tuples of ints (optional). If specified, reduce along the given the dimension(s).
* [in] keepdim: bool (optional). Whether the output has ``dim`` retained or not. Defaults to be False.
----------------
Example-1::
>>> x = jt.randint(10, shape=(2, 3))
>>> x
jt.Var([[7 5 5]
[5 7 5]], dtype=int32)
>>> jt.prod(x)
jt.Var([30625], dtype=int32)
>>> x.prod()
jt.Var([30625], dtype=int32)
>>> x.prod(dim=1)
jt.Var([175 175], dtype=int32)
>>> x.prod(dim=1, keepdims=True)
jt.Var([[175]
[175]], dtype=int32)
*/
// @pybind(prod, product, reduce_multiply)
"multiply",
/**
Tests if all elements in input evaluate to True.
----------------
* [in] x: the input jt.Var.
* [in] dim: int or tuples of ints (optional). If specified, reduce along the given the dimension(s).
* [in] keepdim: bool (optional). Whether the output has ``dim`` retained or not. Defaults to be False.
----------------
Example-1::
>>> x = jt.randint(2, shape=(2, 3))
>>> x
jt.Var([[1 1 1]
[0 1 0]], dtype=int32)
>>> jt.all_(x)
jt.Var([False], dtype=int32)
>>> x.all_()
jt.Var([False], dtype=int32)
>>> x.all_(dim=1)
jt.Var([True False], dtype=int32)
>>> x.all_(dim=1, keepdims=True)
jt.Var([[True]
[False]], dtype=int32)
*/
// @pybind(reduce_logical_and, all_)
"logical_and",
/**
Tests if any elements in input evaluate to True.
----------------
* [in] x: the input jt.Var.
* [in] dim: int or tuples of ints (optional). If specified, reduce along the given the dimension(s).
* [in] keepdim: bool (optional). Whether the output has ``dim`` retained or not. Defaults to be False.
----------------
Example-1::
>>> x = jt.randint(2, shape=(2, 3))
>>> x
jt.Var([[1 0 1]
[0 0 0]], dtype=int32)
>>> jt.any_(x)
jt.Var([True], dtype=int32)
>>> x.any_()
jt.Var([True], dtype=int32)
>>> x.any_(dim=1)
jt.Var([True False], dtype=int32)
>>> x.any_(dim=1, keepdims=True)
jt.Var([[True]
[False]], dtype=int32)
*/
// @pybind(reduce_logical_or, any_)
"logical_or",
"logical_xor",
"bitwise_and",
"bitwise_or",
"bitwise_xor",
/**
Returns the mean value of the input.
----------------
* [in] x: the input jt.Var.
* [in] dim: int or tuples of ints (optional). If specified, reduce along the given the dimension(s).
* [in] keepdim: bool (optional). Whether the output has ``dim`` retained or not. Defaults to be False.
----------------
Example-1::
>>> x = jt.randint(10, shape=(2, 3))
>>> x
jt.Var([[9 4 4]
[1 9 6]], dtype=int32)
>>> jt.mean(x)
jt.Var([5.5000005], dtype=float32)
>>> x.mean()
jt.Var([5.5000005], dtype=float32)
>>> x.mean(dim=1)
jt.Var([5.666667 5.3333335], dtype=float32)
>>> x.mean(dim=1, keepdims=True)
jt.Var([[5.666667 ]
[5.3333335]], dtype=float32)
*/
// @pybind(mean)
"mean",
};

View File

@ -15,6 +15,32 @@ namespace jittor {
struct ReshapeOp : Op {
Var* x, * y;
NanoVector shape;
/**
Returns a tensor with the same data and number of elements as input, but with the specified shape.
A single dimension may be -1, in which case its inferred from the remaining dimensions and the number of elements in input.
----------------
* [in] x: the input jt.Var
* [in] shape: the output shape, an integer array
----------------
Example-1::
>>> a = jt.randint(0, 10, shape=(12,))
>>> a
jt.Var([4 0 8 4 6 3 1 8 1 1 2 2], dtype=int32)
>>> jt.reshape(a, (3, 4))
jt.Var([[4 0 8 4]
[6 3 1 8]
[1 1 2 2]], dtype=int32)
>>> jt.reshape(a, (-1, 6))
jt.Var([[4 0 8 4 6 3]
[1 8 1 1 2 2]], dtype=int32)
*/
ReshapeOp(Var* x, NanoVector shape);
const char* name() const override { return "reshape"; }

View File

@ -37,367 +37,438 @@ static unordered_set<string> unary_ops = {
// please keep float64 the last type
/**
* returns the absolute value of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> jt.abs(jt.float32([-1, 0, 1]))
* jt.Var([1. 0. 1.], dtype=float32)
Returns the absolute value of the input ``x``.
----------------
* [in] x: the input jt.Var
----------------
Example-1::
>>> jt.abs(jt.float32([-1, 0, 1]))
jt.Var([1. 0. 1.], dtype=float32)
*/
// @pybind(abs, __abs__)
"abs",
/**
* returns the negative value of x.
*
* This operator is equavilant to ``-x``.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> jt.negative(jt.float32([-1, 0, 1]))
* jt.Var([ 1. -0. -1.], dtype=float32)
Returns the negative value of the input ``x``.
This operator is equavilant to ``-x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> jt.negative(jt.float32([-1, 0, 1]))
jt.Var([ 1. -0. -1.], dtype=float32)
*/
// @pybind(negative, __neg__)
"negative",
/**
* returns the logical NOT of x.
*
* :param x: the input.
* :type x: jt.Var (integal or boolean types).
*
* Example:
* >>> jt.logical_not(jt.int32([-1, 0, 1]))
* jt.Var([False True False], dtype=bool)
Returns the logical NOT of the input ``x``.
----------------
* [in] x: the input jt.Var, integal or boolean.
----------------
Example-1::
>>> jt.logical_not(jt.int32([-1, 0, 1]))
jt.Var([False True False], dtype=bool)
*/
"logical_not",
/**
* returns the bitwise NOT of x.
*
* :param x: the input.
* :type x: jt.Var (integal or boolean types).
*
* Example:
* >>> jt.bitwise_not(jt.int32([1, 2, -3]))
* jt.Var([-2 -3 2], dtype=int32)
Returns the bitwise NOT of the input ``x``.
----------------
* [in] x: the input jt.Var, integal or boolean.
----------------
Example-1::
>>> jt.bitwise_not(jt.int32([1, 2, -3]))
jt.Var([-2 -3 2], dtype=int32)
*/
"bitwise_not",
/**
* returns the natural logarithm of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.rand(4) * 2
* >>> a
* jt.Var([0.02863695 1.30122 1.6048753 1.140261 ], dtype=float32)
* >>> jt.log(a)
* jt.Var([-3.5530574 0.26330233 0.47304606 0.13125724], dtype=float32)
Returns the natural logarithm of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.rand(4) * 2
>>> a
jt.Var([0.02863695 1.30122 1.6048753 1.140261 ], dtype=float32)
>>> jt.log(a)
jt.Var([-3.5530574 0.26330233 0.47304606 0.13125724], dtype=float32)
*/
"log",
/**
* returns the exponential of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.rand(4) * 2
* >>> a
* jt.Var([1.9841381 1.4103996 0.5855549 1.4212812], dtype=float32)
* >>> jt.exp(a)
* jt.Var([7.2727766 4.0975924 1.7959872 4.1424246], dtype=float32)
Returns the exponential of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.rand(4) * 2
>>> a
jt.Var([1.9841381 1.4103996 0.5855549 1.4212812], dtype=float32)
>>> jt.exp(a)
jt.Var([7.2727766 4.0975924 1.7959872 4.1424246], dtype=float32)
*/
"exp",
/**
* returns the square root of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.rand(4) * 2
* >>> a
* jt.Var([0.81957287 0.5609612 0.07435933 1.7571875 ], dtype=float32)
* >>> jt.sqrt(a)
* jt.Var([0.90530264 0.7489734 0.27268907 1.3255895 ], dtype=float32)
Returns the square root of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.rand(4) * 2
>>> a
jt.Var([0.81957287 0.5609612 0.07435933 1.7571875 ], dtype=float32)
>>> jt.sqrt(a)
jt.Var([0.90530264 0.7489734 0.27268907 1.3255895 ], dtype=float32)
*/
"sqrt",
/**
* returns the closest integer of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.randn(4)
* >>> a
* jt.Var([ 2.101595 0.33055413 -0.44147047 -0.7720668 ], dtype=float32)
* >>> jt.round(a)
* jt.Var([ 2 0 0 -1], dtype=int32)
Returns the closest integer of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.randn(4)
>>> a
jt.Var([ 2.101595 0.33055413 -0.44147047 -0.7720668 ], dtype=float32)
>>> jt.round(a)
jt.Var([ 2 0 0 -1], dtype=int32)
*/
"round",
/**
* returns the largest integer less than or equal to x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.randn(4)
* >>> a
* jt.Var([-1.0339162 -0.7259972 -0.9220003 -0.8449701], dtype=float32)
* >>> jt.floor(a)
* jt.Var([-2 -1 -1 -1], dtype=int32)
Returns the largest integer less than or equal to the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.randn(4)
>>> a
jt.Var([-1.0339162 -0.7259972 -0.9220003 -0.8449701], dtype=float32)
>>> jt.floor(a)
jt.Var([-2 -1 -1 -1], dtype=int32)
*/
"floor",
/**
* returns the smallest integer greater than or equal to x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.randn(4)
* >>> a
* jt.Var([-1.0339162 -0.7259972 -0.9220003 -0.8449701], dtype=float32)
* >>> jt.ceil(a)
* jt.Var([-1 0 0 0], dtype=int32)
Returns the smallest integer greater than or equal to the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.randn(4)
>>> a
jt.Var([-1.0339162 -0.7259972 -0.9220003 -0.8449701], dtype=float32)
>>> jt.ceil(a)
jt.Var([-1 0 0 0], dtype=int32)
*/
"ceil",
/**
* returns the sine of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.randn(4)
* >>> a
* jt.Var([ 0.32893723 -0.7112559 -0.872391 1.8001337 ], dtype=float32)
* >>> jt.sin(a)
* jt.Var([ 0.32303742 -0.6527857 -0.76586854 0.9738172 ], dtype=float32)
Returns the sine of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.randn(4)
>>> a
jt.Var([ 0.32893723 -0.7112559 -0.872391 1.8001337 ], dtype=float32)
>>> jt.sin(a)
jt.Var([ 0.32303742 -0.6527857 -0.76586854 0.9738172 ], dtype=float32)
*/
"sin",
/**
* returns the arcsine of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.randn(4)
* >>> a
* jt.Var([ 0.09342023 -0.42522037 0.9264933 -0.785264 ], dtype=float32)
* >>> jt.asin(a)
* jt.Var([ 0.09355665 -0.43920535 1.1849847 -0.9031224 ], dtype=float32)
Returns the arcsine of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.randn(4)
>>> a
jt.Var([ 0.09342023 -0.42522037 0.9264933 -0.785264 ], dtype=float32)
>>> jt.asin(a)
jt.Var([ 0.09355665 -0.43920535 1.1849847 -0.9031224 ], dtype=float32)
*/
// @pybind(asin, arcsin)
"asin",
/**
* returns the hyperbolic sine of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.randn(4)
* >>> a
* jt.Var([ 0.32893723 -0.7112559 -0.872391 1.8001337 ], dtype=float32)
* >>> jt.sinh(a)
* jt.Var([ 0.3349012 -0.77276015 -0.9873369 2.9425898 ], dtype=float32)
Returns the hyperbolic sine of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.randn(4)
>>> a
jt.Var([ 0.32893723 -0.7112559 -0.872391 1.8001337 ], dtype=float32)
>>> jt.sinh(a)
jt.Var([ 0.3349012 -0.77276015 -0.9873369 2.9425898 ], dtype=float32)
*/
// @pybind(asin, arcsin)
"sinh",
/**
* returns the inverse hyperbolic sine of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.randn(4)
* >>> a
* jt.Var([-1.9749726 -0.52341473 0.8906148 1.0338128 ], dtype=float32)
* >>> jt.asinh(a)
* jt.Var([-1.4323865 -0.5020559 0.8018747 0.90508187], dtype=float32)
Returns the inverse hyperbolic sine of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.randn(4)
>>> a
jt.Var([-1.9749726 -0.52341473 0.8906148 1.0338128 ], dtype=float32)
>>> jt.asinh(a)
jt.Var([-1.4323865 -0.5020559 0.8018747 0.90508187], dtype=float32)
*/
// @pybind(asinh, arcsinh)
"asinh",
/**
* returns the tangent of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.randn(4)
* >>> a
* jt.Var([ 0.32893723 -0.7112559 -0.872391 1.8001337 ], dtype=float32)
* >>> jt.tan(a)
* jt.Var([ 0.34133783 -0.8617148 -1.1910915 -4.283673 ], dtype=float32)
Returns the tangent of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.randn(4)
>>> a
jt.Var([ 0.32893723 -0.7112559 -0.872391 1.8001337 ], dtype=float32)
>>> jt.tan(a)
jt.Var([ 0.34133783 -0.8617148 -1.1910915 -4.283673 ], dtype=float32)
*/
"tan",
/**
* returns the inverse tangent of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.randn(4)
* >>> a
* jt.Var([-0.85885596 1.187804 0.47249675 0.95933187], dtype=float32)
* >>> jt.atan(a)
* jt.Var([-0.70961297 0.87102956 0.44140393 0.76464504], dtype=float32)
Returns the inverse tangent of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.randn(4)
>>> a
jt.Var([-0.85885596 1.187804 0.47249675 0.95933187], dtype=float32)
>>> jt.atan(a)
jt.Var([-0.70961297 0.87102956 0.44140393 0.76464504], dtype=float32)
*/
// @pybind(atan, arctan)
"atan",
/**
* returns the hyperbolic tangent of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.randn(4)
* >>> a
* jt.Var([-0.85885596 1.187804 0.47249675 0.95933187], dtype=float32)
* >>> jt.tanh(a)
* jt.Var([-0.6956678 0.82989657 0.4402144 0.7439787 ], dtype=float32)
Returns the hyperbolic tangent of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.randn(4)
>>> a
jt.Var([-0.85885596 1.187804 0.47249675 0.95933187], dtype=float32)
>>> jt.tanh(a)
jt.Var([-0.6956678 0.82989657 0.4402144 0.7439787 ], dtype=float32)
*/
"tanh",
/**
* returns the inverse hyperbolic tangent of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.rand(4) * 2 - 1
* >>> a
* jt.Var([ 0.9062414 -0.799802 -0.27219176 -0.7274077 ], dtype=float32)
* >>> jt.atanh(a)
* jt.Var([ 1.5060828 -1.0980625 -0.27922946 -0.9231999 ], dtype=float32)
Returns the inverse hyperbolic tangent of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.rand(4) * 2 - 1
>>> a
jt.Var([ 0.9062414 -0.799802 -0.27219176 -0.7274077 ], dtype=float32)
>>> jt.atanh(a)
jt.Var([ 1.5060828 -1.0980625 -0.27922946 -0.9231999 ], dtype=float32)
*/
// @pybind(atanh, arctanh)
"atanh",
/**
* returns the cosine of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.randn(4)
* >>> a
* jt.Var([ 0.32893723 -0.7112559 -0.872391 1.8001337 ], dtype=float32)
* >>> jt.cos(a)
* jt.Var([ 0.9463862 0.7575426 0.6429972 -0.2273323], dtype=float32)
Returns the cosine of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.randn(4)
>>> a
jt.Var([ 0.32893723 -0.7112559 -0.872391 1.8001337 ], dtype=float32)
>>> jt.cos(a)
jt.Var([ 0.9463862 0.7575426 0.6429972 -0.2273323], dtype=float32)
*/
"cos",
/**
* returns the inverse cosine of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.rand(4) * 2 - 1
* >>> a
* jt.Var([ 0.5876564 0.740723 -0.667666 0.5371753], dtype=float32)
* >>> jt.acos(a)
* jt.Var([0.9426371 0.7366504 2.3018656 1.0037117], dtype=float32)
Returns the inverse cosine of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.rand(4) * 2 - 1
>>> a
jt.Var([ 0.5876564 0.740723 -0.667666 0.5371753], dtype=float32)
>>> jt.acos(a)
jt.Var([0.9426371 0.7366504 2.3018656 1.0037117], dtype=float32)
*/
// @pybind(acos, arccos)
"acos",
/**
* returns the hyperbolic cosine of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.randn(4)
* >>> a
* jt.Var([ 0.32893723 -0.7112559 -0.872391 1.8001337 ], dtype=float32)
* >>> jt.cosh(a)
* jt.Var([1.0545894 1.2637873 1.405288 3.1078668], dtype=float32)
Returns the hyperbolic cosine of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.randn(4)
>>> a
jt.Var([ 0.32893723 -0.7112559 -0.872391 1.8001337 ], dtype=float32)
>>> jt.cosh(a)
jt.Var([1.0545894 1.2637873 1.405288 3.1078668], dtype=float32)
*/
"cosh",
/**
* returns the inverse hyperbolic cosine of x.
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.rand(4) + 1
* >>> a
* jt.Var([1.3609099 1.8137748 1.1146184 1.3911307], dtype=float32)
* >>> jt.acosh(a)
* jt.Var([0.8259237 1.2020639 0.47432774 0.8579033 ], dtype=float32)
Returns the inverse hyperbolic cosine of the input ``x``.
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.rand(4) + 1
>>> a
jt.Var([1.3609099 1.8137748 1.1146184 1.3911307], dtype=float32)
>>> jt.acosh(a)
jt.Var([0.8259237 1.2020639 0.47432774 0.8579033 ], dtype=float32)
*/
// @pybind(acosh, arccosh)
"acosh",
/**
* returns the sigmoid of x.
*
* .. math::
* out_i = \frac{1}{1 + e^{x_i}}
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.randn(4)
* >>> a
* jt.Var([ 0.49443012 0.4305426 -1.0364404 -1.2628382 ], dtype=float32)
* >>> jt.sigmoid(a)
* jt.Var([0.62114954 0.6060032 0.2618374 0.2204857 ], dtype=float32)
Returns the sigmoid of the input ``x``.
.. math::
out_i = \frac{1}{1 + e^{x_i}}
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.randn(4)
>>> a
jt.Var([ 0.49443012 0.4305426 -1.0364404 -1.2628382 ], dtype=float32)
>>> jt.sigmoid(a)
jt.Var([0.62114954 0.6060032 0.2618374 0.2204857 ], dtype=float32)
*/
"sigmoid",
/**
* Computes the error function of each element. The error function is defined as follows:
*
* .. math::
* erf(x) = \frac{2}{\sqrt{\pi}} \int_0^x e^{-t^2} dt
*
* :param x: the input.
* :type x: jt.Var.
*
* Example:
* >>> a = jt.randn(4)
* >>> a
* jt.Var([ 0.49443012 0.4305426 -1.0364404 -1.2628382 ], dtype=float32)
* >>> jt.erf(a)
* jt.Var([ 0.51559156 0.45739546 -0.85728306 -0.9258883 ], dtype=float32)
Computes the error function of each element. The error function is defined as follows:
.. math::
erf(x) = \frac{2}{\sqrt{\pi}} \int_0^x e^{-t^2} dt
----------------
* [in] x: the input jt.Var.
----------------
Example-1::
>>> a = jt.randn(4)
>>> a
jt.Var([ 0.49443012 0.4305426 -1.0364404 -1.2628382 ], dtype=float32)
>>> jt.erf(a)
jt.Var([ 0.51559156 0.45739546 -0.85728306 -0.9258883 ], dtype=float32)
*/
"erf",
};