From e5aa9882d048a73c7a258886fcba7f3424a27dbf Mon Sep 17 00:00:00 2001 From: mengxian Date: Sat, 11 Jan 2025 15:33:45 +0800 Subject: [PATCH 1/5] API --- api-examples/operator/.DS_Store | Bin 12292 -> 6148 bytes api-examples/operator/README.md | 37 +- api-examples/operator/ops.exp!!!.ipynb | 8 +- api-examples/operator/ops.expand_dims.ipynb | 220 +++++++++ api-examples/operator/ops.eye.ipynb | 218 +++++++++ api-examples/operator/ops.isnan.ipynb | 2 +- api-examples/operator/ops.isneginf.ipynb | 2 +- api-examples/operator/ops.isposinf.ipynb | 38 +- api-examples/operator/ops.linspace.ipynb | 235 +++++++++ ...ops.maximum.ipynb => ops.maximum!!!.ipynb} | 0 api-examples/operator/ops.meshgrid.ipynb | 459 ++++++++++++++++++ ...ops.minimum.ipynb => ops.minimum!!!.ipynb} | 0 api-examples/operator/ops.moveaxis.ipynb | 249 ++++++++++ api-examples/operator/ops.not_equal.ipynb | 233 +++++++++ api-examples/operator/ops.outer.ipynb | 263 ++++++++++ api-examples/operator/ops.pad.ipynb | 269 ++++++++++ api-examples/operator/ops.real.ipynb | 195 ++++++++ api-examples/operator/ops.reciprocal.ipynb | 195 ++++++++ api-examples/operator/ops.remainder.ipynb | 205 ++++++++ .../operator/ops.repeat_interleave!!!.ipynb | 273 +++++++++++ api-examples/operator/ops.reshape.ipynb | 287 +++++++++++ api-examples/operator/ops.round!!!.ipynb | 270 +++++++++++ api-examples/operator/ops.rsqrt.ipynb | 192 ++++++++ api-examples/operator/ops.sigmoid.ipynb | 159 ++++++ api-examples/operator/ops.stack.ipynb | 244 ++++++++++ api-examples/operator/ops.stop_gradient.ipynb | 102 ++++ api-examples/operator/ops.swapaxes.ipynb | 229 +++++++++ api-examples/operator/ops.tensor_dot.ipynb | 205 ++++++++ api-examples/operator/ops.tile.ipynb | 204 ++++++++ api-examples/operator/ops.topk.ipynb | 259 ++++++++++ api-examples/operator/ops.trace.ipynb | 287 +++++++++++ api-examples/operator/ops.transpose.ipynb | 268 ++++++++++ api-examples/operator/ops.tril.ipynb | 320 ++++++++++++ api-examples/operator/ops.triu.ipynb | 318 ++++++++++++ api-examples/operator/ops.var.ipynb | 250 ++++++++++ api-examples/operator/ops.where.ipynb | 207 ++++++++ api-examples/operator/ops.zeros.ipynb | 245 ++++++++++ api-examples/operator/ops.zeros_like.ipynb | 231 +++++++++ 38 files changed, 7350 insertions(+), 28 deletions(-) create mode 100644 api-examples/operator/ops.expand_dims.ipynb create mode 100644 api-examples/operator/ops.eye.ipynb create mode 100644 api-examples/operator/ops.linspace.ipynb rename api-examples/operator/{ops.maximum.ipynb => ops.maximum!!!.ipynb} (100%) create mode 100644 api-examples/operator/ops.meshgrid.ipynb rename api-examples/operator/{ops.minimum.ipynb => ops.minimum!!!.ipynb} (100%) create mode 100644 api-examples/operator/ops.moveaxis.ipynb create mode 100644 api-examples/operator/ops.not_equal.ipynb create mode 100644 api-examples/operator/ops.outer.ipynb create mode 100644 api-examples/operator/ops.pad.ipynb create mode 100644 api-examples/operator/ops.real.ipynb create mode 100644 api-examples/operator/ops.reciprocal.ipynb create mode 100644 api-examples/operator/ops.remainder.ipynb create mode 100644 api-examples/operator/ops.repeat_interleave!!!.ipynb create mode 100644 api-examples/operator/ops.reshape.ipynb create mode 100644 api-examples/operator/ops.round!!!.ipynb create mode 100644 api-examples/operator/ops.rsqrt.ipynb create mode 100644 api-examples/operator/ops.sigmoid.ipynb create mode 100644 api-examples/operator/ops.stack.ipynb create mode 100644 api-examples/operator/ops.stop_gradient.ipynb create mode 100644 api-examples/operator/ops.swapaxes.ipynb create mode 100644 api-examples/operator/ops.tensor_dot.ipynb create mode 100644 api-examples/operator/ops.tile.ipynb create mode 100644 api-examples/operator/ops.topk.ipynb create mode 100644 api-examples/operator/ops.trace.ipynb create mode 100644 api-examples/operator/ops.transpose.ipynb create mode 100644 api-examples/operator/ops.tril.ipynb create mode 100644 api-examples/operator/ops.triu.ipynb create mode 100644 api-examples/operator/ops.var.ipynb create mode 100644 api-examples/operator/ops.where.ipynb create mode 100644 api-examples/operator/ops.zeros.ipynb create mode 100644 api-examples/operator/ops.zeros_like.ipynb diff --git a/api-examples/operator/.DS_Store b/api-examples/operator/.DS_Store index d121d788895935c3228ce163109c2b45a00585dc..46cc050947f8c4422ae2d16386667947ed2de4a4 100644 GIT binary patch delta 140 zcmZokXfcprU|?W$DortDU=RQ@Ie-{MGqg=C6q~3gIoUvmMH0kkgkYeYFM}(C9z!le zif7JbK}K~(#?4w%4_P-WFwSCJ%+A3f$P7{rG=&>TxB_w8W&c>BkFn$6NKxcl4oG|g zp8$Wtr{s#bVBV`O`L2u-o@pO zNbfdUbMWsxSB@L#X>fjp6Zab;C$c5`GLh%<)5_DU9c$(#cVRpWuIE*^_}B z$_R2N`1?!T?|(h*hWpcQA1Dmj+!tBPA+JpaxuydJp6rd<1a^R_>4 z&qJ$FU=)frK%04r(F8d9dwd*if8L&lHo&C`aB1cvH}edo(b0kH>EqJ$LtC$vfy%%t z1Dojj)Xlx8$fOUDPwycYe})X4_kJe+bCQXReB{dQW9_ieS@81{28hyKAIl0Xgl&$D=t2kUh=RV>NG$2Z7`d(u%KvYMe=A`EY%8DXKVJg9U?qIGU}1!aqXb6JpcM6qj2J(Z z@xs@?p%3ink->nR!1_YK%}byo}w9 zeHVX*Iilb-#(sjgb`w97ZC`t`o}gpY9Kg>5%#p0w=H4Adej*``PYE6VFA|ov<87n9Btot4*r&hwfg-xl1JxD7?rux@P zi~KUO4k06_3%z5}(Hd6qhw33#^IdC{TPq>X3Y2gF&m7{+R`!+Czm1oz4JRrg_l7Y> zd^BgITzvBVu;^5O^4FF$P>=dDj^N*-6%9RMWkgSenSDp+x3=I3E8&(rl_%hKSH3~J zg*Sbaeu3fK!Y?X3RyUZnr!r6(s0>sF{*w$mgjR7EI>6U^e#6gXU&%}#SGj=fu+m@g z`}<A?(sSd3ll%o6==3>^aHil9QhV$KF zmcfMoWevjaGNXZVc*o^%i2ZqXm#iFG{IUiCzmtgq%m7VULdFJ-LMtIviz8$&bd%l0(+h6p%Mou1` z@j2IxWb9a(X6=FV&Sj-`8#AfPp2p5w6aT&(=2@;BzDG%g+n4i2Ru1MQBj@rLbXUs% zF&n*u6UXLlT%P#9|I>%+r7}<%_f^ZRr+Bpb oDITqUibwmodA84c^qq$mcR2p_9|7RU0d@UPR`9z1U(@yfAJq_{4FCWD diff --git a/api-examples/operator/README.md b/api-examples/operator/README.md index 3870848..2e665cc 100644 --- a/api-examples/operator/README.md +++ b/api-examples/operator/README.md @@ -60,7 +60,9 @@ | [ops.erf](./ops.erf.ipynb) | 比较两个输入Tensor是否相等 | element-wise | [ops.erfinv](./ops.erfinv.ipynb) | 计算输入的逆误差函数 | element-wise | [ops.exp](./ops.exp!!!.ipynb) | 计算输入的指数 | element-wise +| [ops.expand_dims](./ops.expand_dims.ipynb) | 对输入在给定的轴上添加额外维度 | Tensors | [ops.expm1](./ops.expm1.ipynb) | 计算输入的指数再减一 | element-wise +| [ops.eye](./ops.eye.ipynb) | 创建一个主对角线上元素为1其余元素为0的Tensor | 创建 | [ops.flatten](./ops.flatten.ipynb) | 沿着start_dim到end_dim的维度对输入Tensor展平 | 神经网络 | [ops.floor_div](./ops.floor_div.ipynb) | 第一个输入除以第二个输入再向下取整 | element-wise | [ops.floor_divide](./ops.floor_divide.ipynb) | 第一个输入除以第二个输入再向下取整 | element-wise @@ -77,6 +79,7 @@ | [ops.isposinf](./ops.isposinf.ipynb) | 判断输入数据的每个元素是否是正无限数 | 比较函数 | [ops.less_equal](./ops.less_equal.ipynb) | 比较第一个输入是否小于等于第二个输入 | 比较函数 | [ops.less](./ops.less.ipynb) | 比较第一个输入是否小于第二个输入 | 比较函数 +| [ops.linspace](./ops.linspace.ipynb) | 返回一个在区间start和end内的包含steps个值的一维Tensor | 创建 | [ops.log](./ops.log.ipynb) | 计算输入的逆误差函数 | element-wise | [ops.log1p](./ops.log1p.ipynb) | 计算输入的逆误差函数 | element-wise | [ops.log2](./ops.log2.ipynb) | 计算输入的逆误差函数 | element-wise @@ -88,20 +91,33 @@ | [ops.logsumexp](./ops.logsumexp.ipynb) | 按指定维度对输入求对数指数和 | element-wise | [ops.matmul](./ops.matmul.ipynb) | 计算两个输入的矩阵乘积 | 线性代数 | [ops.max](./ops.max.ipynb) | 返回给定轴上的最大值及其索引 | Reduction函数 -| [ops.maximum](./ops.maximum.ipynb) | 计算两个输入中的最大值 | 比较函数 +| [ops.maximum](./ops.maximum!!!.ipynb) | 计算两个输入中的最大值 | 比较函数 +| [ops.meshgrid](./ops.meshgrid.ipynb) | 从给定的Tensor生成网格矩阵 | Tensors | [ops.mean](./ops.mean.ipynb) | 返回输入所有元素的平均值 | 比较函数 | [ops.min](./ops.min.ipynb) | 计算两个输入中的最小值 | 比较函数 -| [ops.minimum](./ops.minimum.ipynb) | 返回给定轴上的最小值及其索引 | Reduction函数 +| [ops.minimum](./ops.minimum!!!.ipynb) | 返回给定轴上的最小值及其索引 | Reduction函数 +| [ops.moveaxis](./ops.moveaxis.ipynb) | 将x在source中位置的维度移动到destination中的位置 | 索引、切片、连接、变形 | [ops.mul](./ops.mul.ipynb) | 计算两个Tensor逐元素相乘| element-wise | [ops.multiply](./ops.multiply.ipynb) | 计算两个Tensor逐元素相乘。 | element-wise | [ops.nan_to_num](./ops.nan_to_num!!!.ipynb) | 计算两个Tensor逐元素相乘。 | element-wise | [ops.neg](./ops.neg.ipynb) | 计算输入的相反数 | element-wise | [ops.negative](./ops.negative.ipynb) | 计算输入的相反数 | element-wise +| [ops.not_equal](./ops.not_equal.ipynb) | 计算两个Tensor的元素是否不相等 | 比较函数 | [ops.ones](./ops.ones.ipynb) | 创建一个值全为1的Tensor | element-wise | [ops.ones_like](./ops.ones_like.ipynb) | 返回值为1的Tensor,shape与输入相同 | element-wise +| [ops.outer](./ops.outer.ipynb) | 计算两个输入的外积 | 线性代数 +| [ops.pad](./ops.pad.ipynb) | 对输入进行填充 | 神经网络 | [ops.pow](./ops.pow!!!.ipynb) | 计算input中每个元素的exponent次幂 | element-wise | [ops.prod](./ops.prod.ipynb) | 返回给定维度中输入每一行的乘积 | Reduction函数 -| [ops.rad2deg](./ops.rad2deg.ipynb) | 计算输入的的高斯误差 | 比较函数 +| [ops.rad2deg](./ops.rad2deg.ipynb) | 将输入从弧度制转换为度数制 | element-wise +| [ops.real](./ops.real.ipynb) | 返回输入的实数部分 | element-wise +| [ops.reciprocal](./ops.reciprocal.ipynb) | 返回输入的每个元素的倒数 | element-wise +| [ops.remainder](./ops.remainder.ipynb) |计算第一个输入元素除以第二个输入元素的余数 | element-wise +| [ops.repeat_interleave](./ops.repeat_interleave.ipynb) |按指定轴重复Tensor的元素 | 数学运算 +| [ops.reshape](./ops.reshape.ipynb) | 根据shape对输入进行重新排列 | 索引、切片、连接、变形 +| [ops.round](./ops.round.ipynb) | 返回输入四舍五入后的整数值 | element-wise +| [ops.rsqrt](./ops.rsqrt.ipynb) | 计算输入的平方根倒数 | element-wise +| [ops.sigmoid](./ops.sigmoid.ipynb) | 逐元素计算sigmoid激活函数 | element-wise | [ops.sign](./ops.sign.ipynb) | 返回正负1或0,代表输入的正负或0 | element-wise | [ops.sin](./ops.sin.ipynb) | 计算输入的正弦值 | element-wise | [ops.sinh](./ops.sinh.ipynb) | 计算输入的双曲正弦值 | element-wise @@ -111,10 +127,23 @@ | [ops.sqrt](./ops.sqrt.ipynb) | 返回输入的平方根 | element-wise | [ops.square](./ops.square.ipynb) | 返回输入的平方 | element-wise | [ops.squeeze](./ops.squeeze.ipynb) | 返回删除指定维度中大小为1的维度后的Tensor | 索引、切片、连接、变形 +| [ops.stack](./ops.stack.ipynb) | 在指定轴上对输入进行堆叠 | 索引、切片、连接、变形 | [ops.std](./ops.std.ipynb) | 按指定维度计算标准差 | Reduction函数 +| [ops.stop_gradient](./ops.stop_gradient.ipynb) | 消除某个值对梯度的影响 | 微分函数 | [ops.subtract](./ops.subtract.ipynb) | 对两个输入进行减法操作 | element-wise | [ops.sum](./ops.sum.ipynb) | 按指定维度计算元素和 | Tensor +| [ops.swapaxes](./ops.swapaxes.ipynb) | 交换输入的两个维度 | 索引、切片、连接、变形 | [ops.tan](./ops.tan.ipynb) | 计算输入的正切值 | element-wise | [ops.tanh](./ops.tanh.ipynb) | 计算输入的双曲正切值 | element-wise - +| [ops.tensor_dot](./ops.tensor_dot.ipynb) | 在指定轴上对两个输入进行点乘操作 | 线性代数 +| [ops.tile](./ops.tile.ipynb) | 根据指定维度复制input | 索引、切片、连接、变形 +| [ops.topk](./ops.topk.ipynb) | 沿给定维度查找 k 个最大或最小元素和对应的索引 | 比较函数 +| [ops.trace](./ops.trace.ipynb) | 返回二维输入在主对角线方向上的元素总和 | 线性代数 +| [ops.transpose](./ops.transpose.ipynb) | 按指定的排列对输入进行数据重排 | 索引、切片、连接、变形 +| [ops.tril](./ops.tril.ipynb) | 返回输入的对角线和下面的元素,并将其他元素设置为0 | Tensors +| [ops.triu](./ops.triu.ipynb) | 返回输入的对角线和上面的元素,并将其他元素设置为0 | Tensors +| [ops.var](./ops.var.ipynb) | 按指定维度返回输入Tensor上的方差 | Reduction函数 +| [ops.where](./ops.where.ipynb) | 根据条件从输入中选择元素组成返回 | Tensor +| [ops.zeros_like](./ops.zeros_like.ipynb) | 返回值为0的Tensor,shape与输入相同 | 创建 +| [ops.zeros](./ops.zeros.ipynb) | 创建一个值全为0的Tensor | 创建 diff --git a/api-examples/operator/ops.exp!!!.ipynb b/api-examples/operator/ops.exp!!!.ipynb index ba04dce..e9322c9 100644 --- a/api-examples/operator/ops.exp!!!.ipynb +++ b/api-examples/operator/ops.exp!!!.ipynb @@ -12,7 +12,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "id": "518c829d-80a5-4d98-b90c-09635ae35665", "metadata": {}, "outputs": [ @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 4, "id": "2944c154-2bb7-42cf-85a0-4f34e2332311", "metadata": {}, "outputs": [ @@ -74,7 +74,7 @@ "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[12], line 5\u001b[0m\n\u001b[1;32m 2\u001b[0m input_2 \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m3\u001b[39m])\n\u001b[1;32m 4\u001b[0m y1 \u001b[38;5;241m=\u001b[39m ms\u001b[38;5;241m.\u001b[39mops\u001b[38;5;241m.\u001b[39mexp(ms\u001b[38;5;241m.\u001b[39mtensor(input_1)\u001b[38;5;241m+\u001b[39mms\u001b[38;5;241m.\u001b[39mtensor(input_2))\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;43mprint\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mmindspore output ->\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43my1\u001b[49m\u001b[43m)\u001b[49m\n", + "Cell \u001b[0;32mIn[4], line 5\u001b[0m\n\u001b[1;32m 2\u001b[0m input_2 \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m1\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m3\u001b[39m])\n\u001b[1;32m 4\u001b[0m y1 \u001b[38;5;241m=\u001b[39m ms\u001b[38;5;241m.\u001b[39mops\u001b[38;5;241m.\u001b[39mexp(ms\u001b[38;5;241m.\u001b[39mtensor(input_1)\u001b[38;5;241m+\u001b[39mms\u001b[38;5;241m.\u001b[39mtensor(input_2))\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28;43mprint\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mmindspore output ->\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43my1\u001b[49m\u001b[43m)\u001b[49m\n", "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/common/_stub_tensor.py:48\u001b[0m, in \u001b[0;36m_stub_method..fun\u001b[0;34m(*arg, **kwargs)\u001b[0m\n\u001b[1;32m 46\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mfun\u001b[39m(\u001b[38;5;241m*\u001b[39marg, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m 47\u001b[0m stub \u001b[38;5;241m=\u001b[39m arg[\u001b[38;5;241m0\u001b[39m]\n\u001b[0;32m---> 48\u001b[0m arg \u001b[38;5;241m=\u001b[39m (\u001b[43mstub\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstub_sync\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m,) \u001b[38;5;241m+\u001b[39m arg[\u001b[38;5;241m1\u001b[39m:]\n\u001b[1;32m 49\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m method(\u001b[38;5;241m*\u001b[39marg, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n", "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/common/_stub_tensor.py:159\u001b[0m, in \u001b[0;36mStubTensor.stub_sync\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 157\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"sync real tensor.\"\"\"\u001b[39;00m\n\u001b[1;32m 158\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstub:\n\u001b[0;32m--> 159\u001b[0m val \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstub\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mget_value\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 160\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mtensor \u001b[38;5;241m=\u001b[39m Tensor(val, internal\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 161\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmember_cache\u001b[39m\u001b[38;5;124m\"\u001b[39m):\n", "\u001b[0;31mTypeError\u001b[0m: Select CPU operator[Exp] fail! Unsupported data type!\nThe supported data types are input[Float64], output[Float64];input[Complex64], output[Complex64];input[Complex128], output[Complex128];input[Float32], output[Float32];,\n but get input[[const vector]{Int64}], output[[const vector]{Float32}].\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ops/kernel/common/pyboost/pyboost_utils.cc:556 GetKernelAttr\n" @@ -118,7 +118,7 @@ "id": "4d31be7f-2bda-4b34-839b-2bdbd7457f98", "metadata": {}, "source": [ - "当在输入时做加法操作,ms会报错,且报错行在print处,不太合理。 \n", + "CPU下,ms不支持input为int型,且报错行在print处,不太合理。 \n", "torch与jax都支持。" ] }, diff --git a/api-examples/operator/ops.expand_dims.ipynb b/api-examples/operator/ops.expand_dims.ipynb new file mode 100644 index 0000000..96f62fd --- /dev/null +++ b/api-examples/operator/ops.expand_dims.ipynb @@ -0,0 +1,220 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.expand_dims(input_x, axis) -〉 Tensor\n", + "对输入在给定的轴上添加额外维度。\n", + "- 输入:\n", + " * input_x:mindspore的tensor。\n", + " * axis:int。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore |jax |\n", + "| :----: |:----: |\n", + "| input_x |a |\n", + "| axis |axis |" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[1 2 3]] (1, 3)\n", + "\n", + "\n", + "jax output:\n", + " [[1 2 3]] (1, 3)\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([1, 2, 3])\n", + "\n", + "y1 = ms.ops.expand_dims(ms.tensor(input), 0)\n", + "y2 = jnp.expand_dims(input, 0)\n", + "print ('mindspore output:\\n',y1, y1.shape)\n", + "print('\\n')\n", + "print ('jax output:\\n',y1, y2.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "21aa93b7-45b9-4712-a930-380bd4b6f502", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[1]\n", + " [2]\n", + " [3]] (3, 1)\n", + "\n", + "\n", + "jax output:\n", + " [[1]\n", + " [2]\n", + " [3]] (3, 1)\n" + ] + } + ], + "source": [ + "y1 = ms.ops.expand_dims(ms.tensor(input), 1)\n", + "y2 = jnp.expand_dims(input, 1)\n", + "print ('mindspore output:\\n',y1, y1.shape)\n", + "print('\\n')\n", + "print ('jax output:\\n',y1, y2.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "38ed36b9-a2a1-470f-8937-99db3f318a12", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "jax output:\n", + " [[1]\n", + " [2]\n", + " [3]] (1, 1, 3, 1)\n" + ] + } + ], + "source": [ + "y2 = jnp.expand_dims(input, (0, 1, 3))\n", + "print ('jax output:\\n',y1, y2.shape)" + ] + }, + { + "cell_type": "markdown", + "id": "158c398e-7030-4e58-8bc7-e16ba1cdc0f8", + "metadata": {}, + "source": [ + "* jax的axis参数可以接受传入tuple, ms不接受,仅能传入int。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling ExpandDims with \"ExpandDims()(input_x=, axis=int)\".\nThe valid calling should be: \n\"ExpandDims()(input_x=, axis=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[17], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexpand_dims\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_def.py:2479\u001b[0m, in \u001b[0;36mexpand_dims\u001b[0;34m(input_x, axis)\u001b[0m\n\u001b[1;32m 2443\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mexpand_dims\u001b[39m(input_x, axis):\n\u001b[1;32m 2444\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 2445\u001b[0m \u001b[38;5;124;03m Adds an additional dimension to `input_x` at the given axis, the dimension\u001b[39;00m\n\u001b[1;32m 2446\u001b[0m \u001b[38;5;124;03m of `input_x` should be greater than or equal to 1.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 2477\u001b[0m \u001b[38;5;124;03m [2. 2.]]]\u001b[39;00m\n\u001b[1;32m 2478\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 2479\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mexpand_dims_op\u001b[49m\u001b[43m(\u001b[49m\u001b[43minput_x\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py:5369\u001b[0m, in \u001b[0;36mExpandDims.__call__\u001b[0;34m(self, input_x, axis)\u001b[0m\n\u001b[1;32m 5368\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, input_x, axis):\n\u001b[0;32m-> 5369\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__call__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43minput_x\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:405\u001b[0m, in \u001b[0;36mPrimitive.__call__\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 403\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m should_elim:\n\u001b[1;32m 404\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m output\n\u001b[0;32m--> 405\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_run_op\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:1022\u001b[0m, in \u001b[0;36m_run_op\u001b[0;34m(obj, op_name, args)\u001b[0m\n\u001b[1;32m 1020\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Single op execution function supported by ge in PyNative mode.\"\"\"\u001b[39;00m\n\u001b[1;32m 1021\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent:\n\u001b[0;32m-> 1022\u001b[0m stub \u001b[38;5;241m=\u001b[39m \u001b[43m_pynative_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1023\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_stub(stub)\n\u001b[1;32m 1024\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent\u001b[38;5;241m.\u001b[39mhook(obj, args)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/common/api.py:1423\u001b[0m, in \u001b[0;36m_PyNativeExecutor.run_op_async\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 1413\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrun_op_async\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs):\n\u001b[1;32m 1414\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1415\u001b[0m \u001b[38;5;124;03m Run single op async.\u001b[39;00m\n\u001b[1;32m 1416\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1421\u001b[0m \u001b[38;5;124;03m StubNode, result of run op.\u001b[39;00m\n\u001b[1;32m 1422\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1423\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: Failed calling ExpandDims with \"ExpandDims()(input_x=, axis=int)\".\nThe valid calling should be: \n\"ExpandDims()(input_x=, axis=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.expand_dims(input, 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "expand_dims requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[18], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexpand_dims\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:1624\u001b[0m, in \u001b[0;36mexpand_dims\u001b[0;34m(a, axis)\u001b[0m\n\u001b[1;32m 1559\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mexpand_dims\u001b[39m(a: ArrayLike, axis: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m|\u001b[39m Sequence[\u001b[38;5;28mint\u001b[39m]) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[1;32m 1560\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Insert dimensions of length 1 into array\u001b[39;00m\n\u001b[1;32m 1561\u001b[0m \n\u001b[1;32m 1562\u001b[0m \u001b[38;5;124;03m JAX implementation of :func:`numpy.expand_dims`, implemented via\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1622\u001b[0m \u001b[38;5;124;03m [3]]]], dtype=int32)\u001b[39;00m\n\u001b[1;32m 1623\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1624\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mexpand_dims\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1625\u001b[0m axis \u001b[38;5;241m=\u001b[39m _ensure_index_tuple(axis)\n\u001b[1;32m 1626\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m lax\u001b[38;5;241m.\u001b[39mexpand_dims(a, axis)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: expand_dims requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.expand_dims(input, 0)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,建议ms优化报错信息。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.eye.ipynb b/api-examples/operator/ops.eye.ipynb new file mode 100644 index 0000000..2a3afcc --- /dev/null +++ b/api-examples/operator/ops.eye.ipynb @@ -0,0 +1,218 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.eye(n, m=None, dtype=None) -〉 Tensor\n", + "创建一个主对角线上元素为1,其余元素为0的Tensor。\n", + "- 输入:\n", + " * n:int,行数。\n", + " * m:int,列数。\n", + " * dtype:mindspore.dtype。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| n | n | n |\n", + "| m | m | m |\n", + "| dtype | out | k |\n", + "| | dtype | dtype |\n", + "| | layout | device |\n", + "| | device | |\n", + "| | requires_grad| |\n", + "\n", + "* torch和jax都提供了device参数,用于设置返回值提交到哪个设备上。\n", + "* torch额外提供了out出参,layout用于设置内存存储结构,以及requires_grad设置自动求导是否记录操作。\n", + "* jax额外提供了参数k,用于设置对角线的偏移量。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[1. 0. 0. 0. 0.]\n", + " [0. 1. 0. 0. 0.]\n", + " [0. 0. 1. 0. 0.]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[1., 0., 0., 0., 0.],\n", + " [0., 1., 0., 0., 0.],\n", + " [0., 0., 1., 0., 0.]])\n", + "\n", + "\n", + "jax output:\n", + " [[1. 0. 0. 0. 0.]\n", + " [0. 1. 0. 0. 0.]\n", + " [0. 0. 1. 0. 0.]]\n", + "\n", + "\n", + "jax output with k:\n", + " [[0. 1. 0. 0. 0.]\n", + " [0. 0. 1. 0. 0.]\n", + " [0. 0. 0. 1. 0.]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "y1 = ms.ops.eye(3, 5, dtype = ms.float32)\n", + "y2 = torch.eye(3, 5, dtype = torch.float32)\n", + "y3 = jnp.eye(3, 5, dtype = np.float32)\n", + "y4 = jnp.eye(3, 5, k = 1, dtype = np.float32)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)\n", + "print('\\n')\n", + "print ('jax output with k:\\n',y4)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling Eye with \"Eye()(n=List, m=List, dtype=mindspore.dtype)\".\nThe valid calling should be: \n\"Eye()(n=, m=, dtype=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/wy/5yzfdb6x7pvfxh89zcmf_w780000gn/T/ipykernel_5657/1085676700.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0my1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meye\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/array_func.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(n, m, dtype)\u001b[0m\n\u001b[1;32m 414\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mm\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 415\u001b[0m \u001b[0mm\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 416\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mdtype\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 417\u001b[0m \u001b[0mdtype\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfloat32\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 418\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0meye_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(self, n, m, dtype)\u001b[0m\n\u001b[1;32m 5468\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 5469\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_convert_stub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpyboost_eye\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mm\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype_to_type_id\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Eye'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'dtype'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Eye with \"Eye()(n=List, m=List, dtype=mindspore.dtype)\".\nThe valid calling should be: \n\"Eye()(n=, m=, dtype=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.eye([3], [5])" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "eye() received an invalid combination of arguments - got (list, list), but expected one of:\n * (int n, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)\n * (int n, int m, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[14], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43meye\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: eye() received an invalid combination of arguments - got (list, list), but expected one of:\n * (int n, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)\n * (int n, int m, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)\n" + ] + } + ], + "source": [ + "y2 = torch.eye([3], [5])" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Shapes must be 1D sequences of concrete values of integer type, got ([3],). 'N' argument of jnp.eye().", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[15], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43meye\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:3523\u001b[0m, in \u001b[0;36meye\u001b[0;34m(N, M, k, dtype)\u001b[0m\n\u001b[1;32m 3521\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (offset\u001b[38;5;241m.\u001b[39mshape \u001b[38;5;241m==\u001b[39m () \u001b[38;5;129;01mand\u001b[39;00m dtypes\u001b[38;5;241m.\u001b[39missubdtype(offset\u001b[38;5;241m.\u001b[39mdtype, np\u001b[38;5;241m.\u001b[39minteger)):\n\u001b[1;32m 3522\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mk must be a scalar integer; got \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mk\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m-> 3523\u001b[0m N_int \u001b[38;5;241m=\u001b[39m \u001b[43mcore\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcanonicalize_dim\u001b[49m\u001b[43m(\u001b[49m\u001b[43mN\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mN\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m argument of jnp.eye()\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3524\u001b[0m M_int \u001b[38;5;241m=\u001b[39m N_int \u001b[38;5;28;01mif\u001b[39;00m M \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;28;01melse\u001b[39;00m core\u001b[38;5;241m.\u001b[39mcanonicalize_dim(M, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mM\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m argument of jnp.eye()\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 3525\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m N_int \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mor\u001b[39;00m M_int \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0\u001b[39m:\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/core.py:1700\u001b[0m, in \u001b[0;36mcanonicalize_dim\u001b[0;34m(d, context)\u001b[0m\n\u001b[1;32m 1691\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcanonicalize_dim\u001b[39m(d: DimSize, context: \u001b[38;5;28mstr\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m DimSize:\n\u001b[1;32m 1692\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Canonicalizes and checks for errors in a user-provided shape dimension value.\u001b[39;00m\n\u001b[1;32m 1693\u001b[0m \n\u001b[1;32m 1694\u001b[0m \u001b[38;5;124;03m Args:\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1698\u001b[0m \u001b[38;5;124;03m A canonical dimension value.\u001b[39;00m\n\u001b[1;32m 1699\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1700\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mcanonicalize_shape\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[43md\u001b[49m\u001b[43m,\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcontext\u001b[49m\u001b[43m)\u001b[49m[\u001b[38;5;241m0\u001b[39m]\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/core.py:1689\u001b[0m, in \u001b[0;36mcanonicalize_shape\u001b[0;34m(shape, context)\u001b[0m\n\u001b[1;32m 1687\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[1;32m 1688\u001b[0m \u001b[38;5;28;01mpass\u001b[39;00m\n\u001b[0;32m-> 1689\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m _invalid_shape_error(shape, context)\n", + "\u001b[0;31mTypeError\u001b[0m: Shapes must be 1D sequences of concrete values of integer type, got ([3],). 'N' argument of jnp.eye()." + ] + } + ], + "source": [ + "y3 = jnp.eye([3], [5])" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.isnan.ipynb b/api-examples/operator/ops.isnan.ipynb index 7ae5b75..b1caa88 100644 --- a/api-examples/operator/ops.isnan.ipynb +++ b/api-examples/operator/ops.isnan.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "source": [ "## mindspore.ops.isnan(input) -〉 Tensor\n", - "判断输入数据的每个元素是否是Nan。\n", + "判断输入数据的每个元素是否是Nan值。\n", "\n", "- 输入:input:mindspore的tensor。\n", "- 返回:mindspore的tensor,输出的shape与输入相同,数据类型为bool。" diff --git a/api-examples/operator/ops.isneginf.ipynb b/api-examples/operator/ops.isneginf.ipynb index 547746d..99b46b6 100644 --- a/api-examples/operator/ops.isneginf.ipynb +++ b/api-examples/operator/ops.isneginf.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "source": [ "## mindspore.ops.isneginf(input) -〉 Tensor\n", - "判断输入数据的每个元素是否是负inf。\n", + "判断输入数据的每个元素是否是负无限数。\n", "\n", "- 输入:input:mindspore的tensor。\n", "- 返回:mindspore的tensor,输出的shape与输入相同,数据类型为bool。" diff --git a/api-examples/operator/ops.isposinf.ipynb b/api-examples/operator/ops.isposinf.ipynb index 7d1e587..a7e3914 100644 --- a/api-examples/operator/ops.isposinf.ipynb +++ b/api-examples/operator/ops.isposinf.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "source": [ "## mindspore.ops.isposinf(input) -〉 Tensor\n", - "判断输入数据的每个元素是否是正inf。\n", + "判断输入数据的每个元素是否是正无限数。\n", "\n", "- 输入:input:mindspore的tensor。\n", "- 返回:mindspore的tensor,输出的shape与输入相同,数据类型为bool。" @@ -74,11 +74,9 @@ ] }, { - "cell_type": "code", - "execution_count": null, - "id": "ad5b79c3-70c9-4e86-a3c4-25efab6d23c2", + "cell_type": "markdown", + "id": "b58d38c0-465b-4add-b5e1-ecd0fe978ffc", "metadata": {}, - "outputs": [], "source": [ "使用out的出参方式:" ] @@ -173,50 +171,54 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 10, "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", "metadata": {}, "outputs": [ { "ename": "TypeError", - "evalue": "isnan(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "evalue": "isposinf(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43misnan\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", - "\u001b[0;31mTypeError\u001b[0m: isnan(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + "Cell \u001b[0;32mIn[10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43misposinf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: isposinf(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" ] } ], "source": [ - "y2 = torch.isneginf(input)" + "y2 = torch.isposinf(input)" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 11, "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", "metadata": {}, "outputs": [ { "ename": "TypeError", - "evalue": "isnan requires ndarray or scalar arguments, got at position 0.", + "evalue": "Cannot determine dtype of [2, 4, 3, 1]", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[4], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43misnan\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", - " \u001b[0;31m[... skipping hidden 11 frame]\u001b[0m\n", - "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/ufuncs.py:823\u001b[0m, in \u001b[0;36misnan\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 820\u001b[0m \u001b[38;5;129m@implements\u001b[39m(np\u001b[38;5;241m.\u001b[39misnan, module\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnumpy\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 821\u001b[0m \u001b[38;5;129m@partial\u001b[39m(jit, inline\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 822\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21misnan\u001b[39m(x: ArrayLike, \u001b[38;5;241m/\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m--> 823\u001b[0m \u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43misnan\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 824\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m lax\u001b[38;5;241m.\u001b[39mne(x, x)\n", - "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", - "\u001b[0;31mTypeError\u001b[0m: isnan requires ndarray or scalar arguments, got at position 0." + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/dtypes.py:677\u001b[0m, in \u001b[0;36mdtype\u001b[0;34m(x, canonicalize)\u001b[0m\n\u001b[1;32m 676\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m--> 677\u001b[0m dt \u001b[38;5;241m=\u001b[39m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mresult_type\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 678\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n", + "\u001b[0;31mTypeError\u001b[0m: Field elements must be 2- or 3-tuples, got '2'", + "\nThe above exception was the direct cause of the following exception:\n", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[11], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43misposinf\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/ufuncs.py:811\u001b[0m, in \u001b[0;36m\u001b[0;34m(x, out)\u001b[0m\n\u001b[1;32m 806\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 807\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m lax\u001b[38;5;241m.\u001b[39mfull_like(x, \u001b[38;5;28;01mFalse\u001b[39;00m, dtype\u001b[38;5;241m=\u001b[39mnp\u001b[38;5;241m.\u001b[39mbool_)\n\u001b[1;32m 810\u001b[0m isposinf: UnOp \u001b[38;5;241m=\u001b[39m implements(np\u001b[38;5;241m.\u001b[39misposinf, skip_params\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mout\u001b[39m\u001b[38;5;124m'\u001b[39m])(\n\u001b[0;32m--> 811\u001b[0m \u001b[38;5;28;01mlambda\u001b[39;00m x, \u001b[38;5;241m/\u001b[39m, out\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m: \u001b[43m_isposneginf\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minf\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mout\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 812\u001b[0m )\n\u001b[1;32m 815\u001b[0m isneginf: UnOp \u001b[38;5;241m=\u001b[39m implements(np\u001b[38;5;241m.\u001b[39misneginf, skip_params\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mout\u001b[39m\u001b[38;5;124m'\u001b[39m])(\n\u001b[1;32m 816\u001b[0m \u001b[38;5;28;01mlambda\u001b[39;00m x, \u001b[38;5;241m/\u001b[39m, out\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m: _isposneginf(\u001b[38;5;241m-\u001b[39mnp\u001b[38;5;241m.\u001b[39minf, x, out)\n\u001b[1;32m 817\u001b[0m )\n\u001b[1;32m 820\u001b[0m \u001b[38;5;129m@implements\u001b[39m(np\u001b[38;5;241m.\u001b[39misnan, module\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnumpy\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 821\u001b[0m \u001b[38;5;129m@partial\u001b[39m(jit, inline\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 822\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21misnan\u001b[39m(x: ArrayLike, \u001b[38;5;241m/\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/ufuncs.py:801\u001b[0m, in \u001b[0;36m_isposneginf\u001b[0;34m(infinity, x, out)\u001b[0m\n\u001b[1;32m 799\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m out \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 800\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mout\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m argument to isneginf/isposinf is not supported.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 801\u001b[0m dtype \u001b[38;5;241m=\u001b[39m \u001b[43mdtypes\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdtype\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 802\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m dtypes\u001b[38;5;241m.\u001b[39missubdtype(dtype, np\u001b[38;5;241m.\u001b[39mfloating):\n\u001b[1;32m 803\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m lax\u001b[38;5;241m.\u001b[39meq(x, _constant_like(x, infinity))\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/dtypes.py:679\u001b[0m, in \u001b[0;36mdtype\u001b[0;34m(x, canonicalize)\u001b[0m\n\u001b[1;32m 677\u001b[0m dt \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39mresult_type(x)\n\u001b[1;32m 678\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m err:\n\u001b[0;32m--> 679\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCannot determine dtype of \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mx\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01merr\u001b[39;00m\n\u001b[1;32m 680\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m dt \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m _jax_dtype_set \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m issubdtype(dt, extended):\n\u001b[1;32m 681\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mValue \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mx\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m with dtype \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mdt\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m is not a valid JAX array \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 682\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtype. Only arrays of numeric types are supported by JAX.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "\u001b[0;31mTypeError\u001b[0m: Cannot determine dtype of [2, 4, 3, 1]" ] } ], "source": [ "input = [2, 4, 3, 1]\n", - "y3 = jnp.isnan(input)" + "y3 = jnp.isposinf(input)" ] }, { diff --git a/api-examples/operator/ops.linspace.ipynb b/api-examples/operator/ops.linspace.ipynb new file mode 100644 index 0000000..1cf418e --- /dev/null +++ b/api-examples/operator/ops.linspace.ipynb @@ -0,0 +1,235 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.linspace(start, end, steps) -〉 Tensor\n", + "返回一个在区间start和end内的包含steps个值的一维Tensor。\n", + "- 输入:\n", + " * start:int、float或mindspore的tensor(数据类型为float32)。\n", + " * end:int、float或mindspore的tensor(数据类型为float32)。\n", + " * steps:int或mindspore的tensor,必须为正数。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| start | start | start |\n", + "| end | end | stop |\n", + "| steps | steps | num |\n", + "| | out | endpoint |\n", + "| | dtype | retstep |\n", + "| | layout | dtype |\n", + "| | device | axis |\n", + "| | requires_grad| device |\n", + "\n", + "* torch和jax都提供了device参数,用于设置返回值提交到哪个设备上。\n", + "* torch额外提供了out出参,layout用于设置内存存储结构,以及requires_grad设置自动求导是否记录操作。\n", + "* jax额外提供了endpoint、retstep、axis参数用于更细节的调整返回的数组。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "markdown", + "id": "b9ed4b2f-a909-4fc3-99e9-d201098481fb", + "metadata": {}, + "source": [ + "生成一维数组:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output: [ 0. 2.5 5. 7.5 10. ]\n", + "torch output: tensor([ 0.0000, 2.5000, 5.0000, 7.5000, 10.0000])\n", + "jax output: [ 0. 2.5 5. 7.5 10. ]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "y1 = ms.ops.linspace(0, 10, 5)\n", + "y2 = torch.linspace(0, 10, 5)\n", + "y3 = jnp.linspace(0, 10, 5)\n", + "print ('mindspore output:',y1)\n", + "print ('torch output:',y2)\n", + "print ('jax output:',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "c97b4693-280d-40a8-80d0-ceab6e6e7720", + "metadata": {}, + "source": [ + "生成多维数组:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "21aa93b7-45b9-4712-a930-380bd4b6f502", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "jax output: [[ 0. 5. ]\n", + " [ 1.25 6.25]\n", + " [ 2.5 7.5 ]\n", + " [ 3.75 8.75]\n", + " [ 5. 10. ]]\n" + ] + } + ], + "source": [ + "y3 = jnp.linspace(np.array([0, 5]), np.array([5, 10]), 5)\n", + "print ('jax output:',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "daf3cfb4-5f04-4a54-b634-e53a389ce00f", + "metadata": {}, + "source": [ + "* ms和torch的前两个参数都不支持传入多维tensor,所以无法生成多维tensor。jax支持。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling LinSpace with \"LinSpace()(start=Tensor, stop=Tensor, num=List)\".\nThe valid calling should be: \n\"LinSpace()(start=, stop=, num=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[12], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlinspace\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/math_func.py:2750\u001b[0m, in \u001b[0;36mlinspace\u001b[0;34m(start, end, steps)\u001b[0m\n\u001b[1;32m 2748\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(end, Tensor):\n\u001b[1;32m 2749\u001b[0m end \u001b[38;5;241m=\u001b[39m Tensor(end, mstype\u001b[38;5;241m.\u001b[39mfloat32)\n\u001b[0;32m-> 2750\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mlinspace_\u001b[49m\u001b[43m(\u001b[49m\u001b[43mstart\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mend\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msteps\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py:8804\u001b[0m, in \u001b[0;36mLinSpace.__call__\u001b[0;34m(self, start, stop, num)\u001b[0m\n\u001b[1;32m 8803\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, start, stop, num):\n\u001b[0;32m-> 8804\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__call__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mstart\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstop\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnum\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:405\u001b[0m, in \u001b[0;36mPrimitive.__call__\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 403\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m should_elim:\n\u001b[1;32m 404\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m output\n\u001b[0;32m--> 405\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_run_op\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:1022\u001b[0m, in \u001b[0;36m_run_op\u001b[0;34m(obj, op_name, args)\u001b[0m\n\u001b[1;32m 1020\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Single op execution function supported by ge in PyNative mode.\"\"\"\u001b[39;00m\n\u001b[1;32m 1021\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent:\n\u001b[0;32m-> 1022\u001b[0m stub \u001b[38;5;241m=\u001b[39m \u001b[43m_pynative_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1023\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_stub(stub)\n\u001b[1;32m 1024\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent\u001b[38;5;241m.\u001b[39mhook(obj, args)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/common/api.py:1423\u001b[0m, in \u001b[0;36m_PyNativeExecutor.run_op_async\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 1413\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrun_op_async\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs):\n\u001b[1;32m 1414\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1415\u001b[0m \u001b[38;5;124;03m Run single op async.\u001b[39;00m\n\u001b[1;32m 1416\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1421\u001b[0m \u001b[38;5;124;03m StubNode, result of run op.\u001b[39;00m\n\u001b[1;32m 1422\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1423\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: Failed calling LinSpace with \"LinSpace()(start=Tensor, stop=Tensor, num=List)\".\nThe valid calling should be: \n\"LinSpace()(start=, stop=, num=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.linspace([0], [10], [5])" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "f5bbc478-0bde-48c0-a73c-b754ec056e43", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "linspace() received an invalid combination of arguments - got (list, list, list), but expected one of:\n * (Tensor start, Tensor end, int steps, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)\n * (Number start, Tensor end, int steps, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)\n * (Tensor start, Number end, int steps, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)\n * (Number start, Number end, int steps, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[13], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlinspace\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: linspace() received an invalid combination of arguments - got (list, list, list), but expected one of:\n * (Tensor start, Tensor end, int steps, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)\n * (Number start, Tensor end, int steps, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)\n * (Tensor start, Number end, int steps, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)\n * (Number start, Number end, int steps, *, Tensor out, torch.dtype dtype, torch.layout layout, torch.device device, bool pin_memory, bool requires_grad)\n" + ] + } + ], + "source": [ + "y2 = torch.linspace([0], [10], [5])" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'list' object cannot be interpreted as an integer", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[14], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlinspace\u001b[49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m10\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m5\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:3637\u001b[0m, in \u001b[0;36mlinspace\u001b[0;34m(start, stop, num, endpoint, retstep, dtype, axis)\u001b[0m\n\u001b[1;32m 3632\u001b[0m \u001b[38;5;129m@util\u001b[39m\u001b[38;5;241m.\u001b[39mimplements(np\u001b[38;5;241m.\u001b[39mlinspace)\n\u001b[1;32m 3633\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mlinspace\u001b[39m(start: ArrayLike, stop: ArrayLike, num: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m50\u001b[39m,\n\u001b[1;32m 3634\u001b[0m endpoint: \u001b[38;5;28mbool\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m, retstep: \u001b[38;5;28mbool\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[1;32m 3635\u001b[0m dtype: DTypeLike \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 3636\u001b[0m axis: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array \u001b[38;5;241m|\u001b[39m \u001b[38;5;28mtuple\u001b[39m[Array, Array]:\n\u001b[0;32m-> 3637\u001b[0m num \u001b[38;5;241m=\u001b[39m \u001b[43mcore\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconcrete_or_error\u001b[49m\u001b[43m(\u001b[49m\u001b[43moperator\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mindex\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnum\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mnum\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m argument of jnp.linspace\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3638\u001b[0m axis \u001b[38;5;241m=\u001b[39m core\u001b[38;5;241m.\u001b[39mconcrete_or_error(operator\u001b[38;5;241m.\u001b[39mindex, axis, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124maxis\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m argument of jnp.linspace\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 3639\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _linspace(start, stop, num, endpoint, retstep, dtype, axis)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/core.py:1536\u001b[0m, in \u001b[0;36mconcrete_or_error\u001b[0;34m(force, val, context)\u001b[0m\n\u001b[1;32m 1534\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m ConcretizationTypeError(val, context)\n\u001b[1;32m 1535\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1536\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mforce\u001b[49m\u001b[43m(\u001b[49m\u001b[43mval\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: 'list' object cannot be interpreted as an integer" + ] + } + ], + "source": [ + "y3 = jnp.linspace([0], [10], [5])" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,建议ms优化报错信息。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.maximum.ipynb b/api-examples/operator/ops.maximum!!!.ipynb similarity index 100% rename from api-examples/operator/ops.maximum.ipynb rename to api-examples/operator/ops.maximum!!!.ipynb diff --git a/api-examples/operator/ops.meshgrid.ipynb b/api-examples/operator/ops.meshgrid.ipynb new file mode 100644 index 0000000..f639d2b --- /dev/null +++ b/api-examples/operator/ops.meshgrid.ipynb @@ -0,0 +1,459 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.meshgrid(*inputs, indexing='xy') -〉 Tensor\n", + "从给定的Tensor生成网格矩阵。\n", + "- 输入:\n", + " * inputs:tuple[Tensor], list[Tensor], Tensor为mindspore的tensor。\n", + " * indexing:str枚举值:'xy' 或 'ij'。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| inputs | tensors | xi |\n", + "| indexing | indexing | copy(不可修改)|\n", + "| | | sparse |\n", + "| | | indexing |\n", + "\n", + "* jax额外提供了sparse参数用于将每维度的元素降为1。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " (Tensor(shape=[3, 4, 5], dtype=Int64, value=\n", + "[[[1, 1, 1, 1, 1],\n", + " [2, 2, 2, 2, 2],\n", + " [3, 3, 3, 3, 3],\n", + " [4, 4, 4, 4, 4]],\n", + " [[1, 1, 1, 1, 1],\n", + " [2, 2, 2, 2, 2],\n", + " [3, 3, 3, 3, 3],\n", + " [4, 4, 4, 4, 4]],\n", + " [[1, 1, 1, 1, 1],\n", + " [2, 2, 2, 2, 2],\n", + " [3, 3, 3, 3, 3],\n", + " [4, 4, 4, 4, 4]]]), Tensor(shape=[3, 4, 5], dtype=Int64, value=\n", + "[[[5, 5, 5, 5, 5],\n", + " [5, 5, 5, 5, 5],\n", + " [5, 5, 5, 5, 5],\n", + " [5, 5, 5, 5, 5]],\n", + " [[6, 6, 6, 6, 6],\n", + " [6, 6, 6, 6, 6],\n", + " [6, 6, 6, 6, 6],\n", + " [6, 6, 6, 6, 6]],\n", + " [[7, 7, 7, 7, 7],\n", + " [7, 7, 7, 7, 7],\n", + " [7, 7, 7, 7, 7],\n", + " [7, 7, 7, 7, 7]]]), Tensor(shape=[3, 4, 5], dtype=Int64, value=\n", + "[[[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]]]))\n", + "torch output:\n", + " (tensor([[[1, 1, 1, 1, 1],\n", + " [2, 2, 2, 2, 2],\n", + " [3, 3, 3, 3, 3],\n", + " [4, 4, 4, 4, 4]],\n", + "\n", + " [[1, 1, 1, 1, 1],\n", + " [2, 2, 2, 2, 2],\n", + " [3, 3, 3, 3, 3],\n", + " [4, 4, 4, 4, 4]],\n", + "\n", + " [[1, 1, 1, 1, 1],\n", + " [2, 2, 2, 2, 2],\n", + " [3, 3, 3, 3, 3],\n", + " [4, 4, 4, 4, 4]]]), tensor([[[5, 5, 5, 5, 5],\n", + " [5, 5, 5, 5, 5],\n", + " [5, 5, 5, 5, 5],\n", + " [5, 5, 5, 5, 5]],\n", + "\n", + " [[6, 6, 6, 6, 6],\n", + " [6, 6, 6, 6, 6],\n", + " [6, 6, 6, 6, 6],\n", + " [6, 6, 6, 6, 6]],\n", + "\n", + " [[7, 7, 7, 7, 7],\n", + " [7, 7, 7, 7, 7],\n", + " [7, 7, 7, 7, 7],\n", + " [7, 7, 7, 7, 7]]]), tensor([[[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + "\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + "\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]]]))\n", + "jax output:\n", + " [Array([[[1, 1, 1, 1, 1],\n", + " [2, 2, 2, 2, 2],\n", + " [3, 3, 3, 3, 3],\n", + " [4, 4, 4, 4, 4]],\n", + "\n", + " [[1, 1, 1, 1, 1],\n", + " [2, 2, 2, 2, 2],\n", + " [3, 3, 3, 3, 3],\n", + " [4, 4, 4, 4, 4]],\n", + "\n", + " [[1, 1, 1, 1, 1],\n", + " [2, 2, 2, 2, 2],\n", + " [3, 3, 3, 3, 3],\n", + " [4, 4, 4, 4, 4]]], dtype=int32), Array([[[5, 5, 5, 5, 5],\n", + " [5, 5, 5, 5, 5],\n", + " [5, 5, 5, 5, 5],\n", + " [5, 5, 5, 5, 5]],\n", + "\n", + " [[6, 6, 6, 6, 6],\n", + " [6, 6, 6, 6, 6],\n", + " [6, 6, 6, 6, 6],\n", + " [6, 6, 6, 6, 6]],\n", + "\n", + " [[7, 7, 7, 7, 7],\n", + " [7, 7, 7, 7, 7],\n", + " [7, 7, 7, 7, 7],\n", + " [7, 7, 7, 7, 7]]], dtype=int32), Array([[[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + "\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + "\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]]], dtype=int32)]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "x = np.array([1, 2, 3, 4])\n", + "y = np.array([5, 6, 7])\n", + "z = np.array([8, 9, 0, 1, 2])\n", + "\n", + "y1 = ms.ops.meshgrid(ms.tensor(x), ms.tensor(y), ms.tensor(z), indexing='xy')\n", + "y2 = torch.meshgrid(torch.tensor(x), torch.tensor(y), torch.tensor(z), indexing='xy')\n", + "y3 = jnp.meshgrid(x, y, z, indexing='xy')\n", + "print ('mindspore output:\\n',y1)\n", + "print ('torch output:\\n',y2)\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "21aa93b7-45b9-4712-a930-380bd4b6f502", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " (Tensor(shape=[4, 3, 5], dtype=Int64, value=\n", + "[[[1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1]],\n", + " [[2, 2, 2, 2, 2],\n", + " [2, 2, 2, 2, 2],\n", + " [2, 2, 2, 2, 2]],\n", + " [[3, 3, 3, 3, 3],\n", + " [3, 3, 3, 3, 3],\n", + " [3, 3, 3, 3, 3]],\n", + " [[4, 4, 4, 4, 4],\n", + " [4, 4, 4, 4, 4],\n", + " [4, 4, 4, 4, 4]]]), Tensor(shape=[4, 3, 5], dtype=Int64, value=\n", + "[[[5, 5, 5, 5, 5],\n", + " [6, 6, 6, 6, 6],\n", + " [7, 7, 7, 7, 7]],\n", + " [[5, 5, 5, 5, 5],\n", + " [6, 6, 6, 6, 6],\n", + " [7, 7, 7, 7, 7]],\n", + " [[5, 5, 5, 5, 5],\n", + " [6, 6, 6, 6, 6],\n", + " [7, 7, 7, 7, 7]],\n", + " [[5, 5, 5, 5, 5],\n", + " [6, 6, 6, 6, 6],\n", + " [7, 7, 7, 7, 7]]]), Tensor(shape=[4, 3, 5], dtype=Int64, value=\n", + "[[[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]]]))\n", + "torch output:\n", + " (tensor([[[1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1]],\n", + "\n", + " [[2, 2, 2, 2, 2],\n", + " [2, 2, 2, 2, 2],\n", + " [2, 2, 2, 2, 2]],\n", + "\n", + " [[3, 3, 3, 3, 3],\n", + " [3, 3, 3, 3, 3],\n", + " [3, 3, 3, 3, 3]],\n", + "\n", + " [[4, 4, 4, 4, 4],\n", + " [4, 4, 4, 4, 4],\n", + " [4, 4, 4, 4, 4]]]), tensor([[[5, 5, 5, 5, 5],\n", + " [6, 6, 6, 6, 6],\n", + " [7, 7, 7, 7, 7]],\n", + "\n", + " [[5, 5, 5, 5, 5],\n", + " [6, 6, 6, 6, 6],\n", + " [7, 7, 7, 7, 7]],\n", + "\n", + " [[5, 5, 5, 5, 5],\n", + " [6, 6, 6, 6, 6],\n", + " [7, 7, 7, 7, 7]],\n", + "\n", + " [[5, 5, 5, 5, 5],\n", + " [6, 6, 6, 6, 6],\n", + " [7, 7, 7, 7, 7]]]), tensor([[[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + "\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + "\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + "\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]]]))\n", + "jax output:\n", + " [Array([[[1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1],\n", + " [1, 1, 1, 1, 1]],\n", + "\n", + " [[2, 2, 2, 2, 2],\n", + " [2, 2, 2, 2, 2],\n", + " [2, 2, 2, 2, 2]],\n", + "\n", + " [[3, 3, 3, 3, 3],\n", + " [3, 3, 3, 3, 3],\n", + " [3, 3, 3, 3, 3]],\n", + "\n", + " [[4, 4, 4, 4, 4],\n", + " [4, 4, 4, 4, 4],\n", + " [4, 4, 4, 4, 4]]], dtype=int32), Array([[[5, 5, 5, 5, 5],\n", + " [6, 6, 6, 6, 6],\n", + " [7, 7, 7, 7, 7]],\n", + "\n", + " [[5, 5, 5, 5, 5],\n", + " [6, 6, 6, 6, 6],\n", + " [7, 7, 7, 7, 7]],\n", + "\n", + " [[5, 5, 5, 5, 5],\n", + " [6, 6, 6, 6, 6],\n", + " [7, 7, 7, 7, 7]],\n", + "\n", + " [[5, 5, 5, 5, 5],\n", + " [6, 6, 6, 6, 6],\n", + " [7, 7, 7, 7, 7]]], dtype=int32), Array([[[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + "\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + "\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]],\n", + "\n", + " [[8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2],\n", + " [8, 9, 0, 1, 2]]], dtype=int32)]\n" + ] + } + ], + "source": [ + "y1 = ms.ops.meshgrid(ms.tensor(x), ms.tensor(y), ms.tensor(z), indexing='ij')\n", + "y2 = torch.meshgrid(torch.tensor(x), torch.tensor(y), torch.tensor(z), indexing='ij')\n", + "y3 = jnp.meshgrid(x, y, z, indexing='ij')\n", + "print ('mindspore output:\\n',y1)\n", + "print ('torch output:\\n',y2)\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "daf3cfb4-5f04-4a54-b634-e53a389ce00f", + "metadata": {}, + "source": [ + "* ms返回值的value在最后面,建议关键信息提前。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "The primitive[Meshgrid]'s input arguments[element0, element1, element2] must be all tensor and those type must be same. But got input argument[element0]:External\n But got input argument[element1]:External\n But got input argument[element2]:External\nValid type list: {Tensor[BFloat16], Tensor[Bool], Tensor[Complex128], Tensor[Complex64], Tensor[Float16], Tensor[Float32], Tensor[Float64], Tensor[Int16], Tensor[Int32], Tensor[Int64], Tensor[Int8], Tensor[UInt16], Tensor[UInt32], Tensor[UInt64], Tensor[UInt8]}.\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/core/utils/check_convert_utils.cc:786 CheckTensorTypeSame\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmeshgrid\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mz\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindexing\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mxy\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/array_func.py:4071\u001b[0m, in \u001b[0;36mmeshgrid\u001b[0;34m(indexing, *inputs)\u001b[0m\n\u001b[1;32m 3992\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 3993\u001b[0m \u001b[38;5;124;03mGenerates coordinate matrices from given coordinate tensors.\u001b[39;00m\n\u001b[1;32m 3994\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 4068\u001b[0m \u001b[38;5;124;03m [8, 9, 0, 1, 2]]]))\u001b[39;00m\n\u001b[1;32m 4069\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 4070\u001b[0m meshgrid_op \u001b[38;5;241m=\u001b[39m _get_cache_prim(P\u001b[38;5;241m.\u001b[39mMeshgrid)(indexing)\n\u001b[0;32m-> 4071\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mmeshgrid_op\u001b[49m\u001b[43m(\u001b[49m\u001b[43minputs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:405\u001b[0m, in \u001b[0;36mPrimitive.__call__\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 403\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m should_elim:\n\u001b[1;32m 404\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m output\n\u001b[0;32m--> 405\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_run_op\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:1022\u001b[0m, in \u001b[0;36m_run_op\u001b[0;34m(obj, op_name, args)\u001b[0m\n\u001b[1;32m 1020\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Single op execution function supported by ge in PyNative mode.\"\"\"\u001b[39;00m\n\u001b[1;32m 1021\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent:\n\u001b[0;32m-> 1022\u001b[0m stub \u001b[38;5;241m=\u001b[39m \u001b[43m_pynative_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1023\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_stub(stub)\n\u001b[1;32m 1024\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent\u001b[38;5;241m.\u001b[39mhook(obj, args)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/common/api.py:1423\u001b[0m, in \u001b[0;36m_PyNativeExecutor.run_op_async\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 1413\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrun_op_async\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs):\n\u001b[1;32m 1414\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1415\u001b[0m \u001b[38;5;124;03m Run single op async.\u001b[39;00m\n\u001b[1;32m 1416\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1421\u001b[0m \u001b[38;5;124;03m StubNode, result of run op.\u001b[39;00m\n\u001b[1;32m 1422\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1423\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: The primitive[Meshgrid]'s input arguments[element0, element1, element2] must be all tensor and those type must be same. But got input argument[element0]:External\n But got input argument[element1]:External\n But got input argument[element2]:External\nValid type list: {Tensor[BFloat16], Tensor[Bool], Tensor[Complex128], Tensor[Complex64], Tensor[Float16], Tensor[Float32], Tensor[Float64], Tensor[Int16], Tensor[Int32], Tensor[Int64], Tensor[Int8], Tensor[UInt16], Tensor[UInt32], Tensor[UInt64], Tensor[UInt8]}.\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/core/utils/check_convert_utils.cc:786 CheckTensorTypeSame\n" + ] + } + ], + "source": [ + "y1 = ms.ops.meshgrid(x, y, z, indexing='xy')" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "f5bbc478-0bde-48c0-a73c-b754ec056e43", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "expected Tensor as element 0 in argument 0, but got numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmeshgrid\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mz\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindexing\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mxy\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/torch/functional.py:497\u001b[0m, in \u001b[0;36mmeshgrid\u001b[0;34m(indexing, *tensors)\u001b[0m\n\u001b[1;32m 403\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmeshgrid\u001b[39m(\u001b[38;5;241m*\u001b[39mtensors, indexing: Optional[\u001b[38;5;28mstr\u001b[39m] \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Tuple[Tensor, \u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m]:\n\u001b[1;32m 404\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"Creates grids of coordinates specified by the 1D inputs in `attr`:tensors.\u001b[39;00m\n\u001b[1;32m 405\u001b[0m \n\u001b[1;32m 406\u001b[0m \u001b[38;5;124;03m This is helpful when you want to visualize data over some\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 495\u001b[0m \n\u001b[1;32m 496\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 497\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_meshgrid\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mtensors\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindexing\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mindexing\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/torch/functional.py:512\u001b[0m, in \u001b[0;36m_meshgrid\u001b[0;34m(indexing, *tensors)\u001b[0m\n\u001b[1;32m 507\u001b[0m \u001b[38;5;66;03m# Continue allowing call of old method that takes no indexing\u001b[39;00m\n\u001b[1;32m 508\u001b[0m \u001b[38;5;66;03m# kwarg for forward compatibility reasons.\u001b[39;00m\n\u001b[1;32m 509\u001b[0m \u001b[38;5;66;03m#\u001b[39;00m\n\u001b[1;32m 510\u001b[0m \u001b[38;5;66;03m# Remove this two weeks after landing.\u001b[39;00m\n\u001b[1;32m 511\u001b[0m kwargs \u001b[38;5;241m=\u001b[39m {} \u001b[38;5;28;01mif\u001b[39;00m indexing \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;28;01melse\u001b[39;00m {\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mindexing\u001b[39m\u001b[38;5;124m'\u001b[39m: indexing}\n\u001b[0;32m--> 512\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_VF\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmeshgrid\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtensors\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: expected Tensor as element 0 in argument 0, but got numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.meshgrid(x, y, z, indexing='xy')" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "meshgrid requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmeshgrid\u001b[49m\u001b[43m(\u001b[49m\u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensor\u001b[49m\u001b[43m(\u001b[49m\u001b[43my\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mz\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mindexing\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mxy\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:3757\u001b[0m, in \u001b[0;36mmeshgrid\u001b[0;34m(copy, sparse, indexing, *xi)\u001b[0m\n\u001b[1;32m 3754\u001b[0m \u001b[38;5;129m@util\u001b[39m\u001b[38;5;241m.\u001b[39mimplements(np\u001b[38;5;241m.\u001b[39mmeshgrid, lax_description\u001b[38;5;241m=\u001b[39m_ARRAY_VIEW_DOC)\n\u001b[1;32m 3755\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmeshgrid\u001b[39m(\u001b[38;5;241m*\u001b[39mxi: ArrayLike, copy: \u001b[38;5;28mbool\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m, sparse: \u001b[38;5;28mbool\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[1;32m 3756\u001b[0m indexing: \u001b[38;5;28mstr\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mxy\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28mlist\u001b[39m[Array]:\n\u001b[0;32m-> 3757\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmeshgrid\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mxi\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3758\u001b[0m args \u001b[38;5;241m=\u001b[39m [asarray(x) \u001b[38;5;28;01mfor\u001b[39;00m x \u001b[38;5;129;01min\u001b[39;00m xi]\n\u001b[1;32m 3759\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m copy:\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: meshgrid requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "y3 = jnp.meshgrid(ms.tensor(x), ms.tensor(y), ms.tensor(z), indexing='xy')" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,建议ms简洁化报错信息。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.minimum.ipynb b/api-examples/operator/ops.minimum!!!.ipynb similarity index 100% rename from api-examples/operator/ops.minimum.ipynb rename to api-examples/operator/ops.minimum!!!.ipynb diff --git a/api-examples/operator/ops.moveaxis.ipynb b/api-examples/operator/ops.moveaxis.ipynb new file mode 100644 index 0000000..0400bf1 --- /dev/null +++ b/api-examples/operator/ops.moveaxis.ipynb @@ -0,0 +1,249 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.moveaxis(x, source, destination) -〉 Tensor\n", + "调换 x 中 source 和 destination 两个维度的位置。\n", + "- 输入:\n", + " * x:mindspore的tensor。\n", + " * source:int, sequence[int]。\n", + " * destination:int, sequence[int]。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| x | input | a |\n", + "| source | source | source |\n", + "| destination | destination | destination |\n" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "markdown", + "id": "4b3017ed-6df8-4328-9b44-800c4daf35ac", + "metadata": {}, + "source": [ + "实数计算:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " (2, 4, 5, 3)\n", + "\n", + "\n", + "torch output:\n", + " torch.Size([2, 4, 5, 3])\n", + "\n", + "\n", + "jax output:\n", + " (2, 4, 5, 3)\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.ones((2, 3, 4, 5))\n", + "\n", + "y1 = ms.ops.moveaxis(ms.tensor(input), 1, -1)\n", + "y2 = torch.moveaxis(torch.tensor(input),1,-1)\n", + "y3 = jnp.moveaxis(input, 1, -1)\n", + "print ('mindspore output:\\n',y1.shape)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2.shape)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3.shape)" + ] + }, + { + "cell_type": "markdown", + "id": "85ffb0d4-31ad-4953-89ab-2a467c47ee93", + "metadata": {}, + "source": [ + "复数计算:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "21aa93b7-45b9-4712-a930-380bd4b6f502", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " (4, 5, 3, 2)\n", + "\n", + "\n", + "torch output:\n", + " torch.Size([4, 5, 3, 2])\n", + "\n", + "\n", + "jax output:\n", + " (4, 5, 3, 2)\n" + ] + } + ], + "source": [ + "y1 = ms.ops.moveaxis(ms.tensor(input), (0, 1), (-1, -2))\n", + "y2 = torch.moveaxis(torch.tensor(input),(0, 1), (-1, -2))\n", + "y3 = jnp.moveaxis(input, (0, 1), (-1, -2))\n", + "print ('mindspore output:\\n',y1.shape)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2.shape)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3.shape)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "the input x must be Tensor!", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmoveaxis\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/array_func.py:6519\u001b[0m, in \u001b[0;36mmoveaxis\u001b[0;34m(x, source, destination)\u001b[0m\n\u001b[1;32m 6501\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmoveaxis\u001b[39m(x, source, destination):\n\u001b[1;32m 6502\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 6503\u001b[0m \u001b[38;5;124;03m Alias for `ops.movedim`. Moves axis of an array from source to destination.\u001b[39;00m\n\u001b[1;32m 6504\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 6516\u001b[0m \u001b[38;5;124;03m (4, 5, 3)\u001b[39;00m\n\u001b[1;32m 6517\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 6519\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mmovedim\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43msource\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdestination\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/array_func.py:6490\u001b[0m, in \u001b[0;36mmovedim\u001b[0;34m(x, source, destination)\u001b[0m\n\u001b[1;32m 6450\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmovedim\u001b[39m(x, source, destination):\n\u001b[1;32m 6451\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 6452\u001b[0m \u001b[38;5;124;03m Moves axis of an array from source to destination.\u001b[39;00m\n\u001b[1;32m 6453\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 6488\u001b[0m \u001b[38;5;124;03m (4, 3, 5)\u001b[39;00m\n\u001b[1;32m 6489\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 6490\u001b[0m ndim \u001b[38;5;241m=\u001b[39m \u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrank\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 6491\u001b[0m source \u001b[38;5;241m=\u001b[39m _check_axis_valid(source, ndim)\n\u001b[1;32m 6492\u001b[0m destination \u001b[38;5;241m=\u001b[39m _check_axis_valid(destination, ndim)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/operations/manually_defined/ops_def.py:827\u001b[0m, in \u001b[0;36mrank\u001b[0;34m(input_x)\u001b[0m\n\u001b[1;32m 796\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 797\u001b[0m \u001b[38;5;124;03mReturns the rank of a tensor.\u001b[39;00m\n\u001b[1;32m 798\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 824\u001b[0m \n\u001b[1;32m 825\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 826\u001b[0m rank_op \u001b[38;5;241m=\u001b[39m _get_cache_prim(Rank)()\n\u001b[0;32m--> 827\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mrank_op\u001b[49m\u001b[43m(\u001b[49m\u001b[43minput_x\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/operations/manually_defined/ops_def.py:791\u001b[0m, in \u001b[0;36mRank.__call__\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m 789\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, x):\n\u001b[1;32m 790\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(x, (Tensor, Tensor_)):\n\u001b[0;32m--> 791\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mthe input x must be Tensor!\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 792\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(x\u001b[38;5;241m.\u001b[39mshape)\n", + "\u001b[0;31mTypeError\u001b[0m: the input x must be Tensor!" + ] + } + ], + "source": [ + "y1 = ms.ops.moveaxis(input, (0, 1), (-1, -2))" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "moveaxis() received an invalid combination of arguments - got (numpy.ndarray, tuple, tuple), but expected one of:\n * (Tensor input, int source, int destination)\n didn't match because some of the arguments have invalid types: (!numpy.ndarray!, !tuple of (int, int)!, !tuple of (int, int)!)\n * (Tensor input, tuple of ints source, tuple of ints destination)\n didn't match because some of the arguments have invalid types: (!numpy.ndarray!, !tuple of (int, int)!, !tuple of (int, int)!)\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmoveaxis\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: moveaxis() received an invalid combination of arguments - got (numpy.ndarray, tuple, tuple), but expected one of:\n * (Tensor input, int source, int destination)\n didn't match because some of the arguments have invalid types: (!numpy.ndarray!, !tuple of (int, int)!, !tuple of (int, int)!)\n * (Tensor input, tuple of ints source, tuple of ints destination)\n didn't match because some of the arguments have invalid types: (!numpy.ndarray!, !tuple of (int, int)!, !tuple of (int, int)!)\n" + ] + } + ], + "source": [ + "y2 = torch.moveaxis(input, (0, 1), (-1, -2))" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "moveaxis requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[9], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmoveaxis\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:1724\u001b[0m, in \u001b[0;36mmoveaxis\u001b[0;34m(a, source, destination)\u001b[0m\n\u001b[1;32m 1676\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmoveaxis\u001b[39m(a: ArrayLike, source: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m|\u001b[39m Sequence[\u001b[38;5;28mint\u001b[39m],\n\u001b[1;32m 1677\u001b[0m destination: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m|\u001b[39m Sequence[\u001b[38;5;28mint\u001b[39m]) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[1;32m 1678\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Move an array axis to a new position\u001b[39;00m\n\u001b[1;32m 1679\u001b[0m \n\u001b[1;32m 1680\u001b[0m \u001b[38;5;124;03m JAX implementation of :func:`numpy.moveaxis`, implemented in terms of\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1722\u001b[0m \u001b[38;5;124;03m (4, 5, 3, 2)\u001b[39;00m\n\u001b[1;32m 1723\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1724\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmoveaxis\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1725\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _moveaxis(asarray(a), _ensure_index_tuple(source),\n\u001b[1;32m 1726\u001b[0m _ensure_index_tuple(destination))\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: moveaxis requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.moveaxis(input, (0, 1), (-1, -2))" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.not_equal.ipynb b/api-examples/operator/ops.not_equal.ipynb new file mode 100644 index 0000000..835303f --- /dev/null +++ b/api-examples/operator/ops.not_equal.ipynb @@ -0,0 +1,233 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.not_equal(input, other) -〉 Tensor\n", + "计算两个Tensor的元素是否不相等。\n", + "- 输入:input必须为mindspore的tensor(数据类型为Number)。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | x |\n", + "| other | other | y |\n", + "| | out | |\n", + "\n", + "* torch额外提供了out出参方式" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[False True True]\n", + " [ True False True]\n", + " [ True True False]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[False, True, True],\n", + " [ True, False, True],\n", + " [ True, True, False]])\n", + "\n", + "\n", + "jax output:\n", + " [[False True True]\n", + " [ True False True]\n", + " [ True True False]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[1, 2, 3],\n", + " [4, 5, 6],\n", + " [7, 8, 9]])\n", + "other = np.array([1, 5, 9])\n", + "\n", + "y1 = ms.ops.not_equal(ms.tensor(input), ms.tensor(other))\n", + "y2 = torch.not_equal(torch.tensor(input), torch.tensor(other))\n", + "y3 = jnp.not_equal(input, other)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "21aa93b7-45b9-4712-a930-380bd4b6f502", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " False\n", + "jax output:\n", + " False\n" + ] + } + ], + "source": [ + "y1 = ms.ops.not_equal(1, 1.)\n", + "# y2 = torch.not_equal(1, 1.)\n", + "y3 = jnp.not_equal(1, 1.)\n", + "print ('mindspore output:\\n',y1)\n", + "# print ('torch output:\\n',y2)\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。\n", + "* torch不接受input和other同时为数值型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling NotEqual with \"NotEqual()(input=, other=)\".\nThe valid calling should be: \n\"NotEqual()(input=, other=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/wy/5yzfdb6x7pvfxh89zcmf_w780000gn/T/ipykernel_1531/4251568507.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0my1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnot_equal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_def.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(input, other)\u001b[0m\n\u001b[1;32m 5483\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5484\u001b[0m \u001b[0mSupported\u001b[0m \u001b[0mPlatforms\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5485\u001b[0m \u001b[0;31m`\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0mAscend\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0mGPU\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0mCPU\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;31m`\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5486\u001b[0m \"\"\"\n\u001b[0;32m-> 5487\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mnot_equal_op\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(self, input, other)\u001b[0m\n\u001b[1;32m 10537\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m> 10538\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_convert_stub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpyboost_not_equal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: Failed calling NotEqual with \"NotEqual()(input=, other=)\".\nThe valid calling should be: \n\"NotEqual()(input=, other=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.not_equal(input, other)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "not_equal() received an invalid combination of arguments - got (numpy.ndarray, numpy.ndarray), but expected one of:\n * (Tensor input, Tensor other, *, Tensor out)\n * (Tensor input, Number other, *, Tensor out)\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnot_equal\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: not_equal() received an invalid combination of arguments - got (numpy.ndarray, numpy.ndarray), but expected one of:\n * (Tensor input, Tensor other, *, Tensor out)\n * (Tensor input, Number other, *, Tensor out)\n" + ] + } + ], + "source": [ + "y2 = torch.not_equal(input, other)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "not_equal requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[7], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnot_equal\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother\u001b[49m\u001b[43m)\u001b[49m\n", + " \u001b[0;31m[... skipping hidden 11 frame]\u001b[0m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/ufuncs.py:86\u001b[0m, in \u001b[0;36m_one_to_one_binop..\u001b[0;34m(x1, x2)\u001b[0m\n\u001b[1;32m 84\u001b[0m fn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m x1, x2, \u001b[38;5;241m/\u001b[39m: lax_fn(\u001b[38;5;241m*\u001b[39mpromote_args_numeric(numpy_fn\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m, x1, x2))\n\u001b[1;32m 85\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m---> 86\u001b[0m fn \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mlambda\u001b[39;00m x1, x2, \u001b[38;5;241m/\u001b[39m: lax_fn(\u001b[38;5;241m*\u001b[39m\u001b[43mpromote_args\u001b[49m\u001b[43m(\u001b[49m\u001b[43mnumpy_fn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;18;43m__name__\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx1\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx2\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[1;32m 87\u001b[0m fn\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m=\u001b[39m numpy_fn\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\n\u001b[1;32m 88\u001b[0m fn\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__qualname__\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mjax.numpy.\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mnumpy_fn\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:378\u001b[0m, in \u001b[0;36mpromote_args\u001b[0;34m(fun_name, *args)\u001b[0m\n\u001b[1;32m 376\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpromote_args\u001b[39m(fun_name: \u001b[38;5;28mstr\u001b[39m, \u001b[38;5;241m*\u001b[39margs: ArrayLike) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28mlist\u001b[39m[Array]:\n\u001b[1;32m 377\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Convenience function to apply Numpy argument shape and dtype promotion.\"\"\"\u001b[39;00m\n\u001b[0;32m--> 378\u001b[0m \u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfun_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 379\u001b[0m _check_no_float0s(fun_name, \u001b[38;5;241m*\u001b[39margs)\n\u001b[1;32m 380\u001b[0m check_for_prngkeys(fun_name, \u001b[38;5;241m*\u001b[39margs)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: not_equal requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.not_equal(input, other)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.outer.ipynb b/api-examples/operator/ops.outer.ipynb new file mode 100644 index 0000000..3dce4c1 --- /dev/null +++ b/api-examples/operator/ops.outer.ipynb @@ -0,0 +1,263 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.outer(input, vec2) -〉 Tensor\n", + "计算两个输入的外积。\n", + "- 输入:input和vec2必须为mindspore的一维tensor。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | a |\n", + "| vec2 | vec2 | b |\n", + "| | out | out(unused) |\n", + "\n", + "* torch额外提供了out出参方式" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "markdown", + "id": "86e67144-504e-40a2-b499-5d45c73c4632", + "metadata": {}, + "source": [ + "当input和vec2为1维时:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[ 6 9 -15]\n", + " [ -2 -3 5]\n", + " [ 8 12 -20]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[ 6, 9, -15],\n", + " [ -2, -3, 5],\n", + " [ 8, 12, -20]])\n", + "\n", + "\n", + "jax output:\n", + " [[ 6 9 -15]\n", + " [ -2 -3 5]\n", + " [ 8 12 -20]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([3, -1, 4])\n", + "vec2 = np.array([2, 3, -5])\n", + "\n", + "y1 = ms.ops.outer(ms.tensor(input), ms.tensor(vec2))\n", + "y2 = torch.outer(torch.tensor(input), torch.tensor(vec2))\n", + "y3 = jnp.outer(input, vec2)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)\n" + ] + }, + { + "cell_type": "markdown", + "id": "7340e497-f298-4f7d-ac06-e3ea4230352e", + "metadata": {}, + "source": [ + "当input为2维时:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "8ab43b67-d7a6-4cfc-a35b-6c00551af19e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[ 6 9 -15]\n", + " [ -2 -3 5]\n", + " [ 8 12 -20]\n", + " [ 16 24 -40]\n", + " [ 10 15 -25]\n", + " [ -4 -6 10]]\n", + "\n", + "\n", + "jax output:\n", + " [[ 6 9 -15]\n", + " [ -2 -3 5]\n", + " [ 8 12 -20]\n", + " [ 16 24 -40]\n", + " [ 10 15 -25]\n", + " [ -4 -6 10]]\n" + ] + } + ], + "source": [ + "input = np.array([[3, -1, 4],\n", + " [8, 5, -2]])\n", + "vec2 = np.array([2, 3, -5])\n", + "\n", + "y1 = ms.ops.outer(ms.tensor(input), ms.tensor(vec2))\n", + "#y2 = torch.outer(torch.tensor(input), torch.tensor(vec2))\n", + "y3 = jnp.outer(input, vec2)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "#print ('torch output:\\n',y2)\n", + "#print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms和jax会对多维数组展平后再做内积。torch不接受多维数组,会直接报错。\n", + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "the input input must be Tensor!", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/wy/5yzfdb6x7pvfxh89zcmf_w780000gn/T/ipykernel_2209/3192768166.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0my1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mouter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/math_func.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(input, vec2)\u001b[0m\n\u001b[1;32m 4694\u001b[0m [63 90 99]]\n\u001b[1;32m 4695\u001b[0m \"\"\"\n\u001b[1;32m 4696\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4697\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mTensor\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTensor_\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 4698\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"the input input must be Tensor!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4699\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvec2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mTensor\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTensor_\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4700\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"the input vec2 must be Tensor!\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4701\u001b[0m \u001b[0minput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mTypeError\u001b[0m: the input input must be Tensor!" + ] + } + ], + "source": [ + "y1 = ms.ops.outer(input, other)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "outer(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mouter\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: outer(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.outer(input, other)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "outer requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[11], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mouter\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother\u001b[49m\u001b[43m)\u001b[49m\n", + " \u001b[0;31m[... skipping hidden 11 frame]\u001b[0m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:5494\u001b[0m, in \u001b[0;36mouter\u001b[0;34m(a, b, out)\u001b[0m\n\u001b[1;32m 5492\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m out \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 5493\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mout\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m argument to jnp.outer is not supported.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m-> 5494\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mouter\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 5495\u001b[0m a, b \u001b[38;5;241m=\u001b[39m util\u001b[38;5;241m.\u001b[39mpromote_dtypes(a, b)\n\u001b[1;32m 5496\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m ravel(a)[:, \u001b[38;5;28;01mNone\u001b[39;00m] \u001b[38;5;241m*\u001b[39m ravel(b)[\u001b[38;5;28;01mNone\u001b[39;00m, :]\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: outer requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.outer(input, other)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.pad.ipynb b/api-examples/operator/ops.pad.ipynb new file mode 100644 index 0000000..d656e5e --- /dev/null +++ b/api-examples/operator/ops.pad.ipynb @@ -0,0 +1,269 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.pad(input_x, padding, mode='constant', value=None) -〉Tensor\n", + "对输入进行填充。\n", + "- 输入:\n", + " * input_x: mindspore的tensor。\n", + " * padding: tuple[int], list[int], Tensor。\n", + " * mode: str枚举值包含:'constant' 、 'reflect' 、 'replicate' 或者 'circular'。\n", + " * value: int, float, None。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input_x | input | array |\n", + "| padding | pad | pad_width |\n", + "| mode | mode | mode |\n", + "| value | value | constant_values |\n", + "| | | stat_length |\n", + "| | | end_values |\n", + "| | | reflect_type |\n", + "\n", + "* 对于mode参数,ms与torch的枚举类型一致,与jax提供的枚举值有差异。\n", + "* jax额外提供了stat_length、end_values和reflect_type参数,都用于特定mode情况下计算。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "markdown", + "id": "620068a6-55c7-4bc6-a6fa-ab9395de5da9", + "metadata": {}, + "source": [ + "一维数组:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output: [99 99 10 20 30 40 99 99 99 99]\n", + "torch output: tensor([99, 99, 10, 20, 30, 40, 99, 99, 99, 99])\n", + "jax outputn [99 99 10 20 30 40 99 99 99 99]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([10, 20, 30, 40])\n", + "\n", + "y1 = ms.ops.pad(ms.tensor(input), (2, 4), value=99)\n", + "y2 = torch.nn.functional.pad(torch.tensor(input), (2, 4), value=99)\n", + "y3 = jnp.pad(input, (2, 4), constant_values=99)\n", + "print ('mindspore output:',y1)\n", + "print ('torch output:',y2)\n", + "print ('jax outputn',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "473b1fde-202f-48d4-9ca1-c687120fefb6", + "metadata": {}, + "source": [ + "二维数组:" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "ab0c7d20-410c-451b-8ed8-74efeff201b3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[[[10 20 30 40 30]\n", + " [50 60 70 80 70]\n", + " [10 20 30 40 30]]\n", + "\n", + " [[10 20 30 40 30]\n", + " [50 60 70 80 70]\n", + " [10 20 30 40 30]]]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[[[10, 20, 30, 40, 30],\n", + " [50, 60, 70, 80, 70],\n", + " [10, 20, 30, 40, 30]],\n", + "\n", + " [[10, 20, 30, 40, 30],\n", + " [50, 60, 70, 80, 70],\n", + " [10, 20, 30, 40, 30]]]])\n", + "\n", + "\n", + "jax output:\n", + " [[[[10 20 30 40 30]\n", + " [50 60 70 80 70]\n", + " [10 20 30 40 30]]\n", + "\n", + " [[10 20 30 40 30]\n", + " [50 60 70 80 70]\n", + " [10 20 30 40 30]]]]\n" + ] + } + ], + "source": [ + "input = np.array([[[[10, 20, 30, 40],\n", + " [50, 60, 70, 80]],\n", + " [[10, 20, 30, 40],\n", + " [50, 60, 70, 80]]]])\n", + "\n", + "y1 = ms.ops.pad(ms.tensor(input), [0, 1, 0, 1], mode='reflect')\n", + "y2 = torch.nn.functional.pad(torch.tensor(input), [0, 1, 0, 1], mode='reflect')\n", + "y3 = jnp.pad(input, [[0,0], [0, 0], [0,1], [0, 1]], mode='reflect')\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与torch的第二、三个参数及使用方式一致,与jax不同。\n", + "* m与jax都不返回类型" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "For 'pad', the type of 'input_x' must be Tensor, but got .", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[38], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpad\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/nn_func.py:3856\u001b[0m, in \u001b[0;36mpad\u001b[0;34m(input_x, padding, mode, value)\u001b[0m\n\u001b[1;32m 3746\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 3747\u001b[0m \u001b[38;5;124;03mPads the input tensor according to the padding.\u001b[39;00m\n\u001b[1;32m 3748\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 3853\u001b[0m \u001b[38;5;124;03m [5. 4. 5. 4.]]]]\u001b[39;00m\n\u001b[1;32m 3854\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 3855\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(input_x, Tensor):\n\u001b[0;32m-> 3856\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFor \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpad\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m, the type of \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124minput_x\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m must be Tensor, but got \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(input_x)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 3857\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\u001b[38;5;28misinstance\u001b[39m(padding, (\u001b[38;5;28mtuple\u001b[39m, \u001b[38;5;28mlist\u001b[39m)) \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m padding) \u001b[38;5;129;01mor\u001b[39;00m (\u001b[38;5;28misinstance\u001b[39m(padding, Tensor) \u001b[38;5;129;01mand\u001b[39;00m padding\u001b[38;5;241m.\u001b[39mshape \u001b[38;5;241m==\u001b[39m (\u001b[38;5;241m0\u001b[39m,)):\n\u001b[1;32m 3858\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m input_x\n", + "\u001b[0;31mTypeError\u001b[0m: For 'pad', the type of 'input_x' must be Tensor, but got ." + ] + } + ], + "source": [ + "y1 = ms.ops.pad(input, [0, 1, 0, 1])" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "pad(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[39], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfunctional\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpad\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/torch/nn/functional.py:4522\u001b[0m, in \u001b[0;36mpad\u001b[0;34m(input, pad, mode, value)\u001b[0m\n\u001b[1;32m 4515\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m mode \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mreplicate\u001b[39m\u001b[38;5;124m'\u001b[39m:\n\u001b[1;32m 4516\u001b[0m \u001b[38;5;66;03m# Use slow decomp whose backward will be in terms of index_put.\u001b[39;00m\n\u001b[1;32m 4517\u001b[0m \u001b[38;5;66;03m# importlib is required because the import cannot be top level\u001b[39;00m\n\u001b[1;32m 4518\u001b[0m \u001b[38;5;66;03m# (cycle) and cannot be nested (TS doesn't support)\u001b[39;00m\n\u001b[1;32m 4519\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m importlib\u001b[38;5;241m.\u001b[39mimport_module(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtorch._decomp.decompositions\u001b[39m\u001b[38;5;124m'\u001b[39m)\u001b[38;5;241m.\u001b[39m_replication_pad(\n\u001b[1;32m 4520\u001b[0m \u001b[38;5;28minput\u001b[39m, pad\n\u001b[1;32m 4521\u001b[0m )\n\u001b[0;32m-> 4522\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_C\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_nn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpad\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mpad\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmode\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: pad(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.nn.functional.pad(input, [0, 1, 0, 1])" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "pad requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[40], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpad\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:2785\u001b[0m, in \u001b[0;36mpad\u001b[0;34m(array, pad_width, mode, **kwargs)\u001b[0m\n\u001b[1;32m 2778\u001b[0m \u001b[38;5;129m@util\u001b[39m\u001b[38;5;241m.\u001b[39mimplements(np\u001b[38;5;241m.\u001b[39mpad, lax_description\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\"\"\u001b[39m\u001b[38;5;130;01m\\\u001b[39;00m\n\u001b[1;32m 2779\u001b[0m \u001b[38;5;124mUnlike numpy, JAX \u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mfunction\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m mode\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124ms argument (which is another function) should return\u001b[39m\n\u001b[1;32m 2780\u001b[0m \u001b[38;5;124mthe modified array. This is because Jax arrays are immutable.\u001b[39m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 2783\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpad\u001b[39m(array: ArrayLike, pad_width: PadValueLike[\u001b[38;5;28mint\u001b[39m \u001b[38;5;241m|\u001b[39m Array \u001b[38;5;241m|\u001b[39m np\u001b[38;5;241m.\u001b[39mndarray],\n\u001b[1;32m 2784\u001b[0m mode: \u001b[38;5;28mstr\u001b[39m \u001b[38;5;241m|\u001b[39m Callable[\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m\u001b[38;5;241m.\u001b[39m, Any] \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mconstant\u001b[39m\u001b[38;5;124m\"\u001b[39m, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m-> 2785\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mpad\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43marray\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2786\u001b[0m pad_width \u001b[38;5;241m=\u001b[39m _broadcast_to_pairs(pad_width, ndim(array), \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mpad_width\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 2787\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m pad_width \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mall\u001b[39m(core\u001b[38;5;241m.\u001b[39mis_dim(p[\u001b[38;5;241m0\u001b[39m]) \u001b[38;5;129;01mand\u001b[39;00m core\u001b[38;5;241m.\u001b[39mis_dim(p[\u001b[38;5;241m1\u001b[39m])\n\u001b[1;32m 2788\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m p \u001b[38;5;129;01min\u001b[39;00m pad_width):\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: pad requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.pad(input, [[0,0], [0, 0], [0,1], [0, 1]])" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.real.ipynb b/api-examples/operator/ops.real.ipynb new file mode 100644 index 0000000..2027a19 --- /dev/null +++ b/api-examples/operator/ops.real.ipynb @@ -0,0 +1,195 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.real(input) -〉 Tensor\n", + "返回输入的实数部分。\n", + "- 输入:input必须为mindspore的tensor。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | val |\n" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [ 3. 4. -0.]\n", + "\n", + "\n", + "torch output:\n", + " tensor([3., 4., -0.], dtype=torch.float64)\n", + "\n", + "\n", + "jax output:\n", + " [ 3. 4. -0.]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([3-2j, 4+7j, -2j])\n", + "\n", + "y1 = ms.ops.real(ms.tensor(input))\n", + "y2 = torch.real(torch.tensor(input))\n", + "y3 = jnp.real(input)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling Real with \"Real()(input=)\".\nThe valid calling should be: \n\"Real()(input=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[2], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreal\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_def.py:5923\u001b[0m, in \u001b[0;36mreal\u001b[0;34m(input)\u001b[0m\n\u001b[1;32m 5898\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mreal\u001b[39m(\u001b[38;5;28minput\u001b[39m):\n\u001b[1;32m 5899\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 5900\u001b[0m \u001b[38;5;124;03m Returns a Tensor that is the real part of the input. If input is real, it is returned unchanged.\u001b[39;00m\n\u001b[1;32m 5901\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 5921\u001b[0m \u001b[38;5;124;03m 1.3\u001b[39;00m\n\u001b[1;32m 5922\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 5923\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mreal_op\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py:11510\u001b[0m, in \u001b[0;36mReal.__call__\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 11509\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;28minput\u001b[39m):\n\u001b[0;32m> 11510\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__call__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:405\u001b[0m, in \u001b[0;36mPrimitive.__call__\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 403\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m should_elim:\n\u001b[1;32m 404\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m output\n\u001b[0;32m--> 405\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_run_op\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:1022\u001b[0m, in \u001b[0;36m_run_op\u001b[0;34m(obj, op_name, args)\u001b[0m\n\u001b[1;32m 1020\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Single op execution function supported by ge in PyNative mode.\"\"\"\u001b[39;00m\n\u001b[1;32m 1021\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent:\n\u001b[0;32m-> 1022\u001b[0m stub \u001b[38;5;241m=\u001b[39m \u001b[43m_pynative_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1023\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_stub(stub)\n\u001b[1;32m 1024\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent\u001b[38;5;241m.\u001b[39mhook(obj, args)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/common/api.py:1423\u001b[0m, in \u001b[0;36m_PyNativeExecutor.run_op_async\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 1413\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrun_op_async\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs):\n\u001b[1;32m 1414\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1415\u001b[0m \u001b[38;5;124;03m Run single op async.\u001b[39;00m\n\u001b[1;32m 1416\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1421\u001b[0m \u001b[38;5;124;03m StubNode, result of run op.\u001b[39;00m\n\u001b[1;32m 1422\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1423\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Real with \"Real()(input=)\".\nThe valid calling should be: \n\"Real()(input=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.real(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "real(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreal\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: real(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.real(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "real requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[4], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreal\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + " \u001b[0;31m[... skipping hidden 11 frame]\u001b[0m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/ufuncs.py:755\u001b[0m, in \u001b[0;36mreal\u001b[0;34m(val)\u001b[0m\n\u001b[1;32m 752\u001b[0m \u001b[38;5;129m@implements\u001b[39m(np\u001b[38;5;241m.\u001b[39mreal)\n\u001b[1;32m 753\u001b[0m \u001b[38;5;129m@partial\u001b[39m(jit, inline\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 754\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mreal\u001b[39m(val: ArrayLike, \u001b[38;5;241m/\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m--> 755\u001b[0m \u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mreal\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mval\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 756\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m lax\u001b[38;5;241m.\u001b[39mreal(val) \u001b[38;5;28;01mif\u001b[39;00m np\u001b[38;5;241m.\u001b[39miscomplexobj(val) \u001b[38;5;28;01melse\u001b[39;00m lax\u001b[38;5;241m.\u001b[39masarray(val)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: real requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.real(input)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.reciprocal.ipynb b/api-examples/operator/ops.reciprocal.ipynb new file mode 100644 index 0000000..44e6327 --- /dev/null +++ b/api-examples/operator/ops.reciprocal.ipynb @@ -0,0 +1,195 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.reciprocal(input) -〉 Tensor\n", + "返回输入的每个元素的倒数。\n", + "- 输入:input必须为mindspore的tensor。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | x |\n", + "| | out | |\n", + "\n", + "* torch额外提供了out出参方式" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [1. 0.5 0.25]\n", + "\n", + "\n", + "torch output:\n", + " tensor([1.0000, 0.5000, 0.2500], dtype=torch.float64)\n", + "\n", + "\n", + "jax output:\n", + " [1. 0.5 0.25]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([1.0, 2.0, 4.0])\n", + "\n", + "y1 = ms.ops.reciprocal(ms.tensor(input))\n", + "y2 = torch.reciprocal(torch.tensor(input))\n", + "y3 = jnp.reciprocal(input)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling Reciprocal with \"Reciprocal()(x=)\".\nThe valid calling should be: \n\"Reciprocal()(x=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/wy/5yzfdb6x7pvfxh89zcmf_w780000gn/T/ipykernel_7463/446682931.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0my1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreciprocal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/math_func.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(input)\u001b[0m\n\u001b[1;32m 4655\u001b[0m \u001b[0;34m>>\u001b[0m\u001b[0;34m>\u001b[0m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreciprocal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4656\u001b[0m \u001b[0;34m>>\u001b[0m\u001b[0;34m>\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4657\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1.\u001b[0m \u001b[0;36m0.5\u001b[0m \u001b[0;36m0.25\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4658\u001b[0m \"\"\"\n\u001b[0;32m-> 4659\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mreciprocal_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m 11565\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m> 11566\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_convert_stub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpyboost_reciprocal\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Reciprocal with \"Reciprocal()(x=)\".\nThe valid calling should be: \n\"Reciprocal()(x=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.reciprocal(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "reciprocal(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreciprocal\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: reciprocal(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.reciprocal(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "reciprocal requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[4], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreciprocal\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + " \u001b[0;31m[... skipping hidden 11 frame]\u001b[0m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/ufuncs.py:861\u001b[0m, in \u001b[0;36mreciprocal\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 858\u001b[0m \u001b[38;5;129m@implements\u001b[39m(np\u001b[38;5;241m.\u001b[39mreciprocal, module\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnumpy\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 859\u001b[0m \u001b[38;5;129m@partial\u001b[39m(jit, inline\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 860\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mreciprocal\u001b[39m(x: ArrayLike, \u001b[38;5;241m/\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m--> 861\u001b[0m \u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mreciprocal\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 862\u001b[0m x, \u001b[38;5;241m=\u001b[39m promote_dtypes_inexact(x)\n\u001b[1;32m 863\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m lax\u001b[38;5;241m.\u001b[39minteger_pow(x, \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: reciprocal requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.reciprocal(input)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.remainder.ipynb b/api-examples/operator/ops.remainder.ipynb new file mode 100644 index 0000000..8dbc510 --- /dev/null +++ b/api-examples/operator/ops.remainder.ipynb @@ -0,0 +1,205 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.remainder(input, other) -〉 Tensor\n", + "计算第一个输入元素除以第二个输入元素的余数。\n", + "- 输入:input和other必须为numbers.Number、bool或mindspore的tensor(数据类型为Number)。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | x1 |\n", + "| other | other | x2 |\n", + "| | out | |\n", + "\n", + "* torch额外提供了out出参方式" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[ 1 2 -1]\n", + " [ 0 2 -2]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[ 1, 2, -1],\n", + " [ 0, 2, -2]])\n", + "\n", + "\n", + "jax output:\n", + " [[ 1 2 -1]\n", + " [ 0 2 -2]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[3, -1, 4],\n", + " [8, 5, -2]])\n", + "other = np.array([2, 3, -5])\n", + "\n", + "y1 = ms.ops.remainder(ms.tensor(input), ms.tensor(other))\n", + "y2 = torch.remainder(torch.tensor(input), torch.tensor(other))\n", + "y3 = jnp.remainder(input, other)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling FloorDiv with \"FloorDiv()(input=List, other=)\".\nThe valid calling should be: \n\"FloorDiv()(input=, other=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremainder\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/math_func.py:9127\u001b[0m, in \u001b[0;36mremainder\u001b[0;34m(input, other)\u001b[0m\n\u001b[1;32m 9082\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mremainder\u001b[39m(\u001b[38;5;28minput\u001b[39m, other):\n\u001b[1;32m 9083\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 9084\u001b[0m \u001b[38;5;124;03m Computes the remainder of dividing the first input tensor by the second input tensor element-wise.\u001b[39;00m\n\u001b[1;32m 9085\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 9124\u001b[0m \u001b[38;5;124;03m [2. 1. 0.]\u001b[39;00m\n\u001b[1;32m 9125\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 9127\u001b[0m out \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m-\u001b[39m \u001b[43mtensor_floordiv\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;241m*\u001b[39m other\n\u001b[1;32m 9128\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m out\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py:6248\u001b[0m, in \u001b[0;36mFloorDiv.__call__\u001b[0;34m(self, input, other)\u001b[0m\n\u001b[1;32m 6247\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;28minput\u001b[39m, other):\n\u001b[0;32m-> 6248\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__call__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:405\u001b[0m, in \u001b[0;36mPrimitive.__call__\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 403\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m should_elim:\n\u001b[1;32m 404\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m output\n\u001b[0;32m--> 405\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_run_op\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:1022\u001b[0m, in \u001b[0;36m_run_op\u001b[0;34m(obj, op_name, args)\u001b[0m\n\u001b[1;32m 1020\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Single op execution function supported by ge in PyNative mode.\"\"\"\u001b[39;00m\n\u001b[1;32m 1021\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent:\n\u001b[0;32m-> 1022\u001b[0m stub \u001b[38;5;241m=\u001b[39m \u001b[43m_pynative_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1023\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_stub(stub)\n\u001b[1;32m 1024\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent\u001b[38;5;241m.\u001b[39mhook(obj, args)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/common/api.py:1423\u001b[0m, in \u001b[0;36m_PyNativeExecutor.run_op_async\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 1413\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrun_op_async\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs):\n\u001b[1;32m 1414\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1415\u001b[0m \u001b[38;5;124;03m Run single op async.\u001b[39;00m\n\u001b[1;32m 1416\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1421\u001b[0m \u001b[38;5;124;03m StubNode, result of run op.\u001b[39;00m\n\u001b[1;32m 1422\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1423\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: Failed calling FloorDiv with \"FloorDiv()(input=List, other=)\".\nThe valid calling should be: \n\"FloorDiv()(input=, other=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.remainder(input, other)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "remainder() received an invalid combination of arguments - got (list, numpy.ndarray), but expected one of:\n * (Tensor input, Tensor other, *, Tensor out)\n * (Number self, Tensor other)\n didn't match because some of the arguments have invalid types: (!list of [int, int, int, int]!, !numpy.ndarray!)\n * (Tensor input, Number other, *, Tensor out)\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremainder\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: remainder() received an invalid combination of arguments - got (list, numpy.ndarray), but expected one of:\n * (Tensor input, Tensor other, *, Tensor out)\n * (Number self, Tensor other)\n didn't match because some of the arguments have invalid types: (!list of [int, int, int, int]!, !numpy.ndarray!)\n * (Tensor input, Number other, *, Tensor out)\n" + ] + } + ], + "source": [ + "y2 = torch.remainder(input, other)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "remainder requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[3], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mremainder\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother\u001b[49m\u001b[43m)\u001b[49m\n", + " \u001b[0;31m[... skipping hidden 11 frame]\u001b[0m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/ufuncs.py:690\u001b[0m, in \u001b[0;36mremainder\u001b[0;34m(x1, x2)\u001b[0m\n\u001b[1;32m 687\u001b[0m \u001b[38;5;129m@implements\u001b[39m(np\u001b[38;5;241m.\u001b[39mremainder, module\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mnumpy\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 688\u001b[0m \u001b[38;5;129m@jit\u001b[39m\n\u001b[1;32m 689\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mremainder\u001b[39m(x1: ArrayLike, x2: ArrayLike, \u001b[38;5;241m/\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m--> 690\u001b[0m x1, x2 \u001b[38;5;241m=\u001b[39m \u001b[43mpromote_args_numeric\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mremainder\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx1\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx2\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 691\u001b[0m zero \u001b[38;5;241m=\u001b[39m _constant_like(x1, \u001b[38;5;241m0\u001b[39m)\n\u001b[1;32m 692\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m dtypes\u001b[38;5;241m.\u001b[39missubdtype(x2\u001b[38;5;241m.\u001b[39mdtype, np\u001b[38;5;241m.\u001b[39minteger):\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:385\u001b[0m, in \u001b[0;36mpromote_args_numeric\u001b[0;34m(fun_name, *args)\u001b[0m\n\u001b[1;32m 384\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mpromote_args_numeric\u001b[39m(fun_name: \u001b[38;5;28mstr\u001b[39m, \u001b[38;5;241m*\u001b[39margs: ArrayLike) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28mlist\u001b[39m[Array]:\n\u001b[0;32m--> 385\u001b[0m \u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfun_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 386\u001b[0m _check_no_float0s(fun_name, \u001b[38;5;241m*\u001b[39margs)\n\u001b[1;32m 387\u001b[0m check_for_prngkeys(fun_name, \u001b[38;5;241m*\u001b[39margs)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: remainder requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.remainder(input, other)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.repeat_interleave!!!.ipynb b/api-examples/operator/ops.repeat_interleave!!!.ipynb new file mode 100644 index 0000000..6ef8a28 --- /dev/null +++ b/api-examples/operator/ops.repeat_interleave!!!.ipynb @@ -0,0 +1,273 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.repeat_interleave(input, repeats, axis=None) -〉 Tensor\n", + "按指定轴重复Tensor的元素。\n", + "- 输入:\n", + " * input:mindspore的tensor。\n", + " * repeats:int, tuple, list, mindspore的tensor。\n", + " * axis:int。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | a |\n", + "| repeats | repeats | repeats |\n", + "| axis | dim | axis |\n", + "| | output_size | total_repeat_length |\n", + "\n", + "* torch与jax提供了output_size和total_repeat_length参数,用于设定重复次数。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[1 1 2 2]\n", + " [3 3 4 4]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[1, 1, 2, 2],\n", + " [3, 3, 4, 4]])\n", + "\n", + "\n", + "jax output:\n", + " [[1 1 2 2]\n", + " [3 3 4 4]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[1, 2],\n", + " [3, 4]])\n", + "\n", + "y1 = ms.ops.repeat_interleave(ms.tensor(input), 2, 1)\n", + "y2 = torch.repeat_interleave(torch.tensor(input), 2, 1)\n", + "y3 = jnp.repeat(input, 2, 1)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "50e41829-6783-4032-bd5d-867b97b1dc18", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[1 1 2 2]\n", + " [3 3 4 4]]\n", + "mindspore output:\n", + " tensor([[1, 2],\n", + " [3, 4],\n", + " [1, 2],\n", + " [3, 4]])\n" + ] + } + ], + "source": [ + "y1 = ms.ops.repeat_interleave(input, 2, 1)\n", + "print ('mindspore output:\\n',y1)\n", + "\n", + "y1 = ms.ops.repeat_interleave(torch.tensor(input), 2, 1)\n", + "print ('mindspore output:\\n',y1)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* 实测发现ms可接受array/torch.tensor输入,文档未体现, 且与其他接口不一致。\n", + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "605d20b4-007a-4aee-b0e1-18de4d8cb920", + "metadata": {}, + "source": [ + "设置重复次数:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "6c59013f-bf5a-45e5-b9fa-90f5124a0006", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "jax output:\n", + " [[1 1 2 2 2 2]\n", + " [3 3 4 4 4 4]]\n" + ] + } + ], + "source": [ + "# y2 = torch.repeat_interleave(torch.tensor(input), 2, 1, output_size=6)\n", + "y3 = jnp.repeat(input, 2, 1, total_repeat_length=6)\n", + "# print ('torch output:\\n',y2)\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "015556c3-6814-4a42-92f9-017b9389d6d0", + "metadata": {}, + "source": [ + "除4外torch不能设置其他值,jax可随意设置。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'list' object has no attribute 'repeat'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[20], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrepeat_interleave\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmindspore output:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m,y1)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/array_func.py:6688\u001b[0m, in \u001b[0;36mrepeat_interleave\u001b[0;34m(input, repeats, axis)\u001b[0m\n\u001b[1;32m 6686\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(repeats, Tensor):\n\u001b[1;32m 6687\u001b[0m repeats \u001b[38;5;241m=\u001b[39m TensorToList()(repeats)\n\u001b[0;32m-> 6688\u001b[0m output \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43minput\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrepeat\u001b[49m(repeats, axis)\n\u001b[1;32m 6689\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m output\n", + "\u001b[0;31mAttributeError\u001b[0m: 'list' object has no attribute 'repeat'" + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y1 = ms.ops.repeat_interleave(input, 2, 1)\n", + "print ('mindspore output:\\n',y1)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "repeat_interleave() received an invalid combination of arguments - got (list, int, int), but expected one of:\n * (Tensor input, Tensor repeats, int dim, *, int output_size)\n * (Tensor repeats, *, int output_size)\n didn't match because some of the arguments have invalid types: (!list of [int, int, int, int]!, !int!, int)\n * (Tensor input, int repeats, int dim, *, int output_size)\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[21], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrepeat_interleave\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: repeat_interleave() received an invalid combination of arguments - got (list, int, int), but expected one of:\n * (Tensor input, Tensor repeats, int dim, *, int output_size)\n * (Tensor repeats, *, int output_size)\n didn't match because some of the arguments have invalid types: (!list of [int, int, int, int]!, !int!, int)\n * (Tensor input, int repeats, int dim, *, int output_size)\n" + ] + } + ], + "source": [ + "y2 = torch.repeat_interleave(input, 2, 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "repeat requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[25], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrepeat\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:3886\u001b[0m, in \u001b[0;36mrepeat\u001b[0;34m(a, repeats, axis, total_repeat_length)\u001b[0m\n\u001b[1;32m 3883\u001b[0m \u001b[38;5;129m@util\u001b[39m\u001b[38;5;241m.\u001b[39mimplements(np\u001b[38;5;241m.\u001b[39mrepeat, lax_description\u001b[38;5;241m=\u001b[39m_TOTAL_REPEAT_LENGTH_DOC)\n\u001b[1;32m 3884\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrepeat\u001b[39m(a: ArrayLike, repeats: ArrayLike, axis: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;241m*\u001b[39m,\n\u001b[1;32m 3885\u001b[0m total_repeat_length: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m-> 3886\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mrepeat\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3887\u001b[0m core\u001b[38;5;241m.\u001b[39mis_dim(repeats) \u001b[38;5;129;01mor\u001b[39;00m util\u001b[38;5;241m.\u001b[39mcheck_arraylike(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrepeat\u001b[39m\u001b[38;5;124m\"\u001b[39m, repeats)\n\u001b[1;32m 3889\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m axis \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: repeat requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.repeat(input, 2, 1)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.reshape.ipynb b/api-examples/operator/ops.reshape.ipynb new file mode 100644 index 0000000..62642b4 --- /dev/null +++ b/api-examples/operator/ops.reshape.ipynb @@ -0,0 +1,287 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.reshape(input, shape) -〉 Tensor\n", + "根据shape对输入进行重新排列。\n", + "- 输入:\n", + " * input:mindspore的tensor。\n", + " * shape:tuple[int], list[int], Tensor[int]。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | a |\n", + "| shape | shape | shape |\n", + "| | | order |\n", + "| | | copy (unused) |\n", + "| | | newshape (deprecated)|\n", + "\n", + "* jax提供了order参数, 用于设置按行排序还是按列排序。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [1 2 3 4 5 6]\n", + "\n", + "\n", + "torch output:\n", + " tensor([1, 2, 3, 4, 5, 6])\n", + "\n", + "\n", + "jax output:\n", + " [1 2 3 4 5 6]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[1, 2, 3],\n", + " [4, 5, 6]])\n", + "\n", + "y1 = ms.ops.reshape(ms.tensor(input), (6, ))\n", + "y2 = torch.reshape(torch.tensor(input), (6, ))\n", + "y3 = jnp.reshape(input, 6)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "35498b4d-901f-4dd5-906c-9499b9cb67c0", + "metadata": {}, + "source": [ + "jax可接受shape为int, ms与torch不接受。" + ] + }, + { + "cell_type": "markdown", + "id": "63b7959a-df90-4ded-a514-d08a0871840b", + "metadata": {}, + "source": [ + "使用-1自动计算出应有的shape,如:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "bc808ec9-b81f-4ee3-9d3b-d419b2aba7b2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[1 2]\n", + " [3 4]\n", + " [5 6]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[1, 2],\n", + " [3, 4],\n", + " [5, 6]])\n", + "\n", + "\n", + "jax output:\n", + " [[1 2]\n", + " [3 4]\n", + " [5 6]]\n" + ] + } + ], + "source": [ + "y1 = ms.ops.reshape(ms.tensor(input), (-1, 2))\n", + "y2 = torch.reshape(torch.tensor(input), (-1, 2))\n", + "y3 = jnp.reshape(input, (-1, 2))\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "jax.numpy.reshape的order参数使用:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "ba24fde6-2a06-454b-9914-36b1f70c1f1a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "order F output:\n", + " [[1 5]\n", + " [4 3]\n", + " [2 6]]\n", + "\n", + "\n", + "order C output:\n", + " [[1 2]\n", + " [3 4]\n", + " [5 6]]\n" + ] + } + ], + "source": [ + "y1 = jnp.reshape(input, (-1, 2), order='F')\n", + "y2 = jnp.reshape(input, (-1, 2), order='C')\n", + "print ('order F output:\\n',y1)\n", + "print('\\n')\n", + "print ('order C output:\\n',y2)" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling Reshape with \"Reshape()(input=, shape=Tuple)\".\nThe valid calling should be: \n\"Reshape()(input=, shape=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/wy/5yzfdb6x7pvfxh89zcmf_w780000gn/T/ipykernel_1118/3937914839.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0my1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_def.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(input, shape)\u001b[0m\n\u001b[1;32m 6148\u001b[0m [[-0.1 0.3]\n\u001b[1;32m 6149\u001b[0m \u001b[0;34m[\u001b[0m \u001b[0;36m3.6\u001b[0m \u001b[0;36m0.4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6150\u001b[0m [ 0.5 -3.2]]\n\u001b[1;32m 6151\u001b[0m \"\"\"\n\u001b[0;32m-> 6152\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mreshape_op\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshape\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(self, input, shape)\u001b[0m\n\u001b[1;32m 12668\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshape\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m> 12669\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_convert_stub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpyboost_reshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mshape\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Reshape with \"Reshape()(input=, shape=Tuple)\".\nThe valid calling should be: \n\"Reshape()(input=, shape=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.reshape(input, (-1, 2))" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "reshape(): argument 'input' (position 1) must be Tensor, not list", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[17], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreshape\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: reshape(): argument 'input' (position 1) must be Tensor, not list" + ] + } + ], + "source": [ + "y2 = torch.reshape(input, (-1, 2))" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "reshape requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[16], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mreshape\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + " \u001b[0;31m[... skipping hidden 1 frame]\u001b[0m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: reshape requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.reshape(input, (-1, 2))" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.round!!!.ipynb b/api-examples/operator/ops.round!!!.ipynb new file mode 100644 index 0000000..c3f0d86 --- /dev/null +++ b/api-examples/operator/ops.round!!!.ipynb @@ -0,0 +1,270 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.round(input, *, decimals=0) -〉 Tensor\n", + "返回输入四舍五入后的整数值。\n", + "- 输入:\n", + " * input:mindspore的tensor。\n", + " * decimals:int, 要保留的小数位数。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | a |\n", + "| decimals | decimals | decimals |\n", + "| | out | out(unused) |\n", + "\n", + "* torch额外提供了out出参方式。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " " + ] + }, + { + "ename": "RuntimeError", + "evalue": "For Round only support decimals equal 0, but got decimals equal 2\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ops/kernel/cpu/round_cpu_kernel.cc:50 LaunchKernel\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mRuntimeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[11], line 9\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[38;5;241m1.532\u001b[39m, \u001b[38;5;241m3.267\u001b[39m, \u001b[38;5;241m6.149\u001b[39m])\n\u001b[1;32m 8\u001b[0m y1 \u001b[38;5;241m=\u001b[39m ms\u001b[38;5;241m.\u001b[39mops\u001b[38;5;241m.\u001b[39mround(ms\u001b[38;5;241m.\u001b[39mtensor(\u001b[38;5;28minput\u001b[39m), decimals\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m)\n\u001b[0;32m----> 9\u001b[0m \u001b[38;5;28;43mprint\u001b[39;49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mmindspore output:\u001b[39;49m\u001b[38;5;130;43;01m\\n\u001b[39;49;00m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43my1\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/common/_stub_tensor.py:49\u001b[0m, in \u001b[0;36m_stub_method..fun\u001b[0;34m(*arg, **kwargs)\u001b[0m\n\u001b[1;32m 47\u001b[0m stub \u001b[38;5;241m=\u001b[39m arg[\u001b[38;5;241m0\u001b[39m]\n\u001b[1;32m 48\u001b[0m arg \u001b[38;5;241m=\u001b[39m (stub\u001b[38;5;241m.\u001b[39mstub_sync(),) \u001b[38;5;241m+\u001b[39m arg[\u001b[38;5;241m1\u001b[39m:]\n\u001b[0;32m---> 49\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mmethod\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43marg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/common/tensor.py:518\u001b[0m, in \u001b[0;36mTensor.__str__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 516\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdtype \u001b[38;5;241m==\u001b[39m mstype\u001b[38;5;241m.\u001b[39mtype_none:\n\u001b[1;32m 517\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUnknown Tensor type!\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m--> 518\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mstr\u001b[39m(\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43masnumpy\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/common/tensor.py:1070\u001b[0m, in \u001b[0;36mTensor.asnumpy\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1068\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mhas_init:\n\u001b[1;32m 1069\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minit_data()\n\u001b[0;32m-> 1070\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mTensor_\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43masnumpy\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mRuntimeError\u001b[0m: For Round only support decimals equal 0, but got decimals equal 2\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ops/kernel/cpu/round_cpu_kernel.cc:50 LaunchKernel\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([1.532, 3.267, 6.149])\n", + "\n", + "y1 = ms.ops.round(ms.tensor(input), decimals=2)\n", + "print ('mindspore output:\\n',y1)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "bc808ec9-b81f-4ee3-9d3b-d419b2aba7b2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch output:\n", + " tensor([1.5300, 3.2700, 6.1500], dtype=torch.float64)\n", + "\n", + "\n", + "jax output:\n", + " [1.53 3.27 6.15]\n" + ] + } + ], + "source": [ + "y2 = torch.round(torch.tensor(input), decimals=2)\n", + "y3 = jnp.round(input, 2)\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* 当设置decimals=2时,报错只支持decimals为0,不太合理。jax与torch均能配置。\n", + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "767da381-2fda-49eb-bab7-1b60779e6f80", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [10. 22. 12. 32.]\n", + "\n", + "\n", + "torch output:\n", + " tensor([10., 22., 12., 32.], dtype=torch.float64)\n", + "\n", + "\n", + "jax output:\n", + " [10. 22. 12. 32.]\n" + ] + } + ], + "source": [ + "input = np.array([10.5, 21.5, 12.5, 31.5])\n", + "\n", + "y1 = ms.ops.round(ms.tensor(input))\n", + "y2 = torch.round(torch.tensor(input))\n", + "y3 = jnp.round(input)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "2c4ae9bc-f2ef-4710-8db2-17c2d6767192", + "metadata": {}, + "source": [ + "面对0.5的情况,三种框架表现一致。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling Round with \"Round()(input=, decimals=int)\".\nThe valid calling should be: \n\"Round()(input=, decimals=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/wy/5yzfdb6x7pvfxh89zcmf_w780000gn/T/ipykernel_674/2559532544.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0my1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mround\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/math_func.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(input, decimals)\u001b[0m\n\u001b[1;32m 11950\u001b[0m \u001b[0;34m>>\u001b[0m\u001b[0;34m>\u001b[0m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mround\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdecimals\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11951\u001b[0m \u001b[0;34m>>\u001b[0m\u001b[0;34m>\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11952\u001b[0m \u001b[0;34m[\u001b[0m \u001b[0;36m0.8\u001b[0m \u001b[0;36m1.5\u001b[0m \u001b[0;36m2.4\u001b[0m \u001b[0;36m2.5\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m4.6\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 11953\u001b[0m \"\"\"\n\u001b[0;32m> 11954\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mround_op\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdecimals\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(self, input, decimals)\u001b[0m\n\u001b[1;32m 13473\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdecimals\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m> 13474\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_convert_stub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpyboost_round\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdecimals\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Round with \"Round()(input=, decimals=int)\".\nThe valid calling should be: \n\"Round()(input=, decimals=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.round(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "round(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[15], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mround\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: round(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.round(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "round requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mround\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + " \u001b[0;31m[... skipping hidden 11 frame]\u001b[0m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:2215\u001b[0m, in \u001b[0;36mround\u001b[0;34m(a, decimals, out)\u001b[0m\n\u001b[1;32m 2212\u001b[0m \u001b[38;5;129m@util\u001b[39m\u001b[38;5;241m.\u001b[39mimplements(np\u001b[38;5;241m.\u001b[39maround, skip_params\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mout\u001b[39m\u001b[38;5;124m'\u001b[39m])\n\u001b[1;32m 2213\u001b[0m \u001b[38;5;129m@partial\u001b[39m(jit, static_argnames\u001b[38;5;241m=\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdecimals\u001b[39m\u001b[38;5;124m'\u001b[39m,))\n\u001b[1;32m 2214\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mround\u001b[39m(a: ArrayLike, decimals: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m, out: \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m-> 2215\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mround\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2216\u001b[0m decimals \u001b[38;5;241m=\u001b[39m core\u001b[38;5;241m.\u001b[39mconcrete_or_error(operator\u001b[38;5;241m.\u001b[39mindex, decimals, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdecimals\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m argument of jnp.round\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 2217\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m out \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: round requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.round(input)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.rsqrt.ipynb b/api-examples/operator/ops.rsqrt.ipynb new file mode 100644 index 0000000..134b86b --- /dev/null +++ b/api-examples/operator/ops.rsqrt.ipynb @@ -0,0 +1,192 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.rsqrt(input) -〉 Tensor\n", + "计算输入的平方根倒数。\n", + "- 输入:mindspore的tensor。\n", + "- 返回:mindspore的tensor,与input的shape相同。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| x | input | x |\n", + "| | out | |\n", + "\n", + "* torch额外提供了out出参方式" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [ nan 1.83493961 0.80530021 nan]\n", + "\n", + "\n", + "torch output:\n", + " tensor([ nan, 1.8349, 0.8053, nan], dtype=torch.float64)\n", + "\n", + "\n", + "jax output:\n", + " [ nan 1.8349396 0.80530024 nan]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.lax as jlx\n", + "\n", + "input = np.array([-0.0370, 0.2970, 1.5420, -0.9105])\n", + "\n", + "y1 = ms.ops.rsqrt(ms.tensor(input))\n", + "y2 = torch.rsqrt(torch.tensor(input))\n", + "y3 = jlx.rsqrt(input)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling Rsqrt with \"Rsqrt()(input=)\".\nThe valid calling should be: \n\"Rsqrt()(input=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/wy/5yzfdb6x7pvfxh89zcmf_w780000gn/T/ipykernel_94880/2021468678.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0my1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrsqrt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_def.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(input)\u001b[0m\n\u001b[1;32m 6550\u001b[0m \u001b[0;34m>>\u001b[0m\u001b[0;34m>\u001b[0m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mrsqrt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6551\u001b[0m \u001b[0;34m>>\u001b[0m\u001b[0;34m>\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6552\u001b[0m \u001b[0;34m[\u001b[0m \u001b[0mnan\u001b[0m \u001b[0;36m1.8349396\u001b[0m \u001b[0;36m0.8053002\u001b[0m \u001b[0mnan\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6553\u001b[0m \"\"\"\n\u001b[0;32m-> 6554\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mrsqrt_op\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 13520\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m> 13521\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_convert_stub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpyboost_rsqrt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Rsqrt with \"Rsqrt()(input=)\".\nThe valid calling should be: \n\"Rsqrt()(input=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.rsqrt(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "rsqrt(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrsqrt\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: rsqrt(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.rsqrt(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'jnp' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[4], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241m.\u001b[39mrsqrt(\u001b[38;5;28minput\u001b[39m)\n", + "\u001b[0;31mNameError\u001b[0m: name 'jnp' is not defined" + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.rsqrt(input)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.sigmoid.ipynb b/api-examples/operator/ops.sigmoid.ipynb new file mode 100644 index 0000000..f7dedc0 --- /dev/null +++ b/api-examples/operator/ops.sigmoid.ipynb @@ -0,0 +1,159 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.sigmoid(input) -〉 Tensor\n", + "逐元素计算sigmoid激活函数。\n", + "- 输入:input:mindspore的tensor。\n", + "- 返回:mindspore的tensor, 数据类型和shape与input相同。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch |\n", + "| :----: | :----: |\n", + "| input | input |\n", + "| | out |" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [0.9933072 0.748137 0.2919773 0.14575401]\n", + "\n", + "\n", + "torch output:\n", + " tensor([0.9933, 0.7481, 0.2920, 0.1458])\n" + ] + } + ], + "source": [ + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = [ 5, 1.0887, -0.8858, -1.7683]\n", + "\n", + "y1 = ms.ops.sigmoid(ms.tensor(input))\n", + "y2 = torch.sigmoid(torch.tensor(input))\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)" + ] + }, + { + "cell_type": "markdown", + "id": "adfa1a27-0e91-4155-afa0-c2c039658a68", + "metadata": {}, + "source": [ + "* ms不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling Sigmoid with \"Sigmoid()(input=List)\".\nThe valid calling should be: \n\"Sigmoid()(input=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/wy/5yzfdb6x7pvfxh89zcmf_w780000gn/T/ipykernel_98592/3298440826.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0my1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msigmoid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_def.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(input)\u001b[0m\n\u001b[1;32m 6917\u001b[0m \u001b[0;34m>>\u001b[0m\u001b[0;34m>\u001b[0m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msigmoid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6918\u001b[0m \u001b[0;34m>>\u001b[0m\u001b[0;34m>\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6919\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m0.7310586\u001b[0m \u001b[0;36m0.880797\u001b[0m \u001b[0;36m0.95257413\u001b[0m \u001b[0;36m0.98201376\u001b[0m \u001b[0;36m0.9933072\u001b[0m \u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6920\u001b[0m \"\"\"\n\u001b[0;32m-> 6921\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0msigmoid_op\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 13978\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m> 13979\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_convert_stub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpyboost_sigmoid\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Sigmoid with \"Sigmoid()(input=List)\".\nThe valid calling should be: \n\"Sigmoid()(input=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.sigmoid(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "sigmoid(): argument 'input' (position 1) must be Tensor, not list", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msigmoid\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: sigmoid(): argument 'input' (position 1) must be Tensor, not list" + ] + } + ], + "source": [ + "y2 = torch.sigmoid(input)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.stack.ipynb b/api-examples/operator/ops.stack.ipynb new file mode 100644 index 0000000..78dc74c --- /dev/null +++ b/api-examples/operator/ops.stack.ipynb @@ -0,0 +1,244 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.stack(tensors, axis=0) -〉 Tensor\n", + "在指定轴上对输入进行堆叠。\n", + "- 输入:\n", + " * tensors:由多个mindspore的tensor组成的tuple或list。\n", + " * axis:int。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| tensors | tensors | array |\n", + "| axis | dim | axis |\n", + "| | out | out(unused) | \n", + "| | | dtype |\n", + "\n", + "* torch额外提供了out出参方式。\n", + "* jax可以通过dtype设置返回的数据类型。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[1 2 3]\n", + " [4 5 6]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[1, 2, 3],\n", + " [4, 5, 6]])\n", + "\n", + "\n", + "jax output:\n", + " [[1 2 3]\n", + " [4 5 6]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "x = np.array([1, 2, 3])\n", + "y = np.array([4, 5, 6])\n", + "\n", + "y1 = ms.ops.stack((ms.tensor(x), ms.tensor(y)))\n", + "y2 = torch.stack((torch.tensor(x), torch.tensor(y)))\n", + "y3 = jnp.stack([x, y])\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "e1b41ec0-9275-4732-9a85-d14890398f7c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[1 4]\n", + " [2 5]\n", + " [3 6]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[1, 4],\n", + " [2, 5],\n", + " [3, 6]])\n", + "\n", + "\n", + "jax output:\n", + " [[1 4]\n", + " [2 5]\n", + " [3 6]]\n" + ] + } + ], + "source": [ + "y1 = ms.ops.stack((ms.tensor(x), ms.tensor(y)), 1)\n", + "y2 = torch.stack((torch.tensor(x), torch.tensor(y)), 1)\n", + "y3 = jnp.stack([x, y], 1)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Infer type failed.\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ops/infer/stack.cc:122 StackInferType\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[24], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstack\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/array_func.py:1858\u001b[0m, in \u001b[0;36mstack\u001b[0;34m(tensors, axis)\u001b[0m\n\u001b[1;32m 1822\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1823\u001b[0m \u001b[38;5;124;03mStacks a list of tensors in specified axis.\u001b[39;00m\n\u001b[1;32m 1824\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1855\u001b[0m \u001b[38;5;124;03m [2. 3.]]\u001b[39;00m\n\u001b[1;32m 1856\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1857\u001b[0m _stack \u001b[38;5;241m=\u001b[39m _get_cache_prim(P\u001b[38;5;241m.\u001b[39mStack)(axis)\n\u001b[0;32m-> 1858\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_stack\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtensors\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:405\u001b[0m, in \u001b[0;36mPrimitive.__call__\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 403\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m should_elim:\n\u001b[1;32m 404\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m output\n\u001b[0;32m--> 405\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_run_op\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:1022\u001b[0m, in \u001b[0;36m_run_op\u001b[0;34m(obj, op_name, args)\u001b[0m\n\u001b[1;32m 1020\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Single op execution function supported by ge in PyNative mode.\"\"\"\u001b[39;00m\n\u001b[1;32m 1021\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent:\n\u001b[0;32m-> 1022\u001b[0m stub \u001b[38;5;241m=\u001b[39m \u001b[43m_pynative_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1023\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_stub(stub)\n\u001b[1;32m 1024\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent\u001b[38;5;241m.\u001b[39mhook(obj, args)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/common/api.py:1423\u001b[0m, in \u001b[0;36m_PyNativeExecutor.run_op_async\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 1413\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrun_op_async\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs):\n\u001b[1;32m 1414\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1415\u001b[0m \u001b[38;5;124;03m Run single op async.\u001b[39;00m\n\u001b[1;32m 1416\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1421\u001b[0m \u001b[38;5;124;03m StubNode, result of run op.\u001b[39;00m\n\u001b[1;32m 1422\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1423\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: Infer type failed.\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ops/infer/stack.cc:122 StackInferType\n" + ] + } + ], + "source": [ + "y1 = ms.ops.stack(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "stack(): argument 'tensors' (position 1) must be tuple of Tensors, not method", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[27], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstack\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: stack(): argument 'tensors' (position 1) must be tuple of Tensors, not method" + ] + } + ], + "source": [ + "y2 = torch.stack(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "stack requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[29], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstack\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:2835\u001b[0m, in \u001b[0;36mstack\u001b[0;34m(arrays, axis, out, dtype)\u001b[0m\n\u001b[1;32m 2833\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m concatenate(expand_dims(arrays, axis \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m1\u001b[39m), axis\u001b[38;5;241m=\u001b[39maxis, dtype\u001b[38;5;241m=\u001b[39mdtype)\n\u001b[1;32m 2834\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 2835\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mstack\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43marrays\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2836\u001b[0m shape0 \u001b[38;5;241m=\u001b[39m shape(arrays[\u001b[38;5;241m0\u001b[39m])\n\u001b[1;32m 2837\u001b[0m axis \u001b[38;5;241m=\u001b[39m _canonicalize_axis(axis, \u001b[38;5;28mlen\u001b[39m(shape0) \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m1\u001b[39m)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: stack requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "y3 = jnp.stack(torch.tensor(x))" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.stop_gradient.ipynb b/api-examples/operator/ops.stop_gradient.ipynb new file mode 100644 index 0000000..b542965 --- /dev/null +++ b/api-examples/operator/ops.stop_gradient.ipynb @@ -0,0 +1,102 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.stop_gradient(value) -〉 Tensor\n", + "消除某个值对梯度的影响。\n", + "- 输入:Any。\n", + "- 返回:Any。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | jax |\n", + "| :----: | :----: |\n", + "| value | x |\n" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output with grad:\n", + " 6.0\n", + "jax output with grad:\n", + " 6.0\n", + "mindspore output with no grad:\n", + " 0.0\n", + "jax output with no grad:\n", + " 0.0\n" + ] + } + ], + "source": [ + "import mindspore as ms\n", + "import jax\n", + "\n", + "def f1(x):\n", + " return x ** 2\n", + "\n", + "x = np.float32(3.0)\n", + "\n", + "y1 = ms.ops.grad(f1)(ms.tensor(x))\n", + "y2 = jax.grad(f1)(x)\n", + "print ('mindspore output with grad:\\n',y1)\n", + "print ('jax output with grad:\\n',y2)\n", + "\n", + "def f2(x):\n", + " return jax.lax.stop_gradient(x) ** 2\n", + "\n", + "def f3(x):\n", + " return ms.ops.stop_gradient(x) ** 2\n", + "\n", + "y3 = ms.ops.grad(f3)(ms.tensor(x))\n", + "y4 = jax.grad(f2)(x)\n", + "print ('mindspore output with no grad:\\n',y3)\n", + "print ('jax output with no grad:\\n',y4)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.swapaxes.ipynb b/api-examples/operator/ops.swapaxes.ipynb new file mode 100644 index 0000000..4ea8098 --- /dev/null +++ b/api-examples/operator/ops.swapaxes.ipynb @@ -0,0 +1,229 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.swapaxes(input, axis0, axis1) -〉 Tensor\n", + "交换输入的两个维度。\n", + "- 输入:\n", + " * input:mindspore的tensor。\n", + " * axis0:int。\n", + " * axis1:int。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | a |\n", + "| axis0 | axis0 | axis1 |\n", + "| axis1 | axis1 | axis2 |\n" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "input shape: (2, 2, 3)\n", + "mindspore output:\n", + " [[[ 0 6]\n", + " [ 3 9]]\n", + "\n", + " [[ 1 7]\n", + " [ 4 10]]\n", + "\n", + " [[ 2 8]\n", + " [ 5 11]]]\n", + "output shape: (3, 2, 2)\n", + "\n", + "\n", + "torch output:\n", + " tensor([[[ 0, 6],\n", + " [ 3, 9]],\n", + "\n", + " [[ 1, 7],\n", + " [ 4, 10]],\n", + "\n", + " [[ 2, 8],\n", + " [ 5, 11]]])\n", + "output shape: torch.Size([3, 2, 2])\n", + "\n", + "\n", + "jax output:\n", + " [[[ 0 6]\n", + " [ 3 9]]\n", + "\n", + " [[ 1 7]\n", + " [ 4 10]]\n", + "\n", + " [[ 2 8]\n", + " [ 5 11]]]\n", + "output shape: torch.Size([3, 2, 2])\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[[0, 1, 2],\n", + " [3, 4, 5]],\n", + "\n", + " [[6, 7, 8],\n", + " [9, 10, 11]]])\n", + "\n", + "y1 = ms.ops.swapaxes(ms.tensor(input), 0, 2)\n", + "y2 = torch.swapaxes(torch.tensor(input), 0, 2)\n", + "y3 = jnp.swapaxes(input, 0, 2)\n", + "print('input shape:', input.shape)\n", + "print ('mindspore output:\\n',y1)\n", + "print('output shape:', y1.shape)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('output shape:', y2.shape)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)\n", + "print('output shape:', y2.shape)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "For ops.swapaxes, parameter `input` must be Tensor, but got ", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mswapaxes\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/array_func.py:6557\u001b[0m, in \u001b[0;36mswapaxes\u001b[0;34m(input, axis0, axis1)\u001b[0m\n\u001b[1;32m 6528\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m'''\u001b[39;00m\n\u001b[1;32m 6529\u001b[0m \u001b[38;5;124;03mInterchange two axes of a tensor.\u001b[39;00m\n\u001b[1;32m 6530\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 6554\u001b[0m \u001b[38;5;124;03m (4, 3, 2)\u001b[39;00m\n\u001b[1;32m 6555\u001b[0m \u001b[38;5;124;03m'''\u001b[39;00m\n\u001b[1;32m 6556\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28minput\u001b[39m, Tensor):\n\u001b[0;32m-> 6557\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\n\u001b[1;32m 6558\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mFor ops.swapaxes, parameter `input` must be Tensor, but got \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(\u001b[38;5;28minput\u001b[39m)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 6560\u001b[0m axis0, axis1 \u001b[38;5;241m=\u001b[39m _check_swapaxes_axis((axis0, axis1), \u001b[38;5;28minput\u001b[39m\u001b[38;5;241m.\u001b[39mndim)\n\u001b[1;32m 6561\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m axis0 \u001b[38;5;241m==\u001b[39m axis1:\n", + "\u001b[0;31mTypeError\u001b[0m: For ops.swapaxes, parameter `input` must be Tensor, but got " + ] + } + ], + "source": [ + "y1 = ms.ops.swapaxes(input, 0, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "swapaxes(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mswapaxes\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: swapaxes(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.swapaxes(input, 0, 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "swapaxes requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[9], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mswapaxes\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\n", + " \u001b[0;31m[... skipping hidden 11 frame]\u001b[0m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:1670\u001b[0m, in \u001b[0;36mswapaxes\u001b[0;34m(a, axis1, axis2)\u001b[0m\n\u001b[1;32m 1629\u001b[0m \u001b[38;5;129m@partial\u001b[39m(jit, static_argnames\u001b[38;5;241m=\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124maxis1\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124maxis2\u001b[39m\u001b[38;5;124m'\u001b[39m), inline\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 1630\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mswapaxes\u001b[39m(a: ArrayLike, axis1: \u001b[38;5;28mint\u001b[39m, axis2: \u001b[38;5;28mint\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[1;32m 1631\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Swap two axes of an array.\u001b[39;00m\n\u001b[1;32m 1632\u001b[0m \n\u001b[1;32m 1633\u001b[0m \u001b[38;5;124;03m JAX implementation of :func:`numpy.swapaxes`, implemented in terms of\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1668\u001b[0m \u001b[38;5;124;03m (2, 5, 4, 3)\u001b[39;00m\n\u001b[1;32m 1669\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1670\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mswapaxes\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1671\u001b[0m perm \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marange(ndim(a))\n\u001b[1;32m 1672\u001b[0m perm[axis1], perm[axis2] \u001b[38;5;241m=\u001b[39m perm[axis2], perm[axis1]\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: swapaxes requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.swapaxes(input, 0, 2)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.tensor_dot.ipynb b/api-examples/operator/ops.tensor_dot.ipynb new file mode 100644 index 0000000..36eb890 --- /dev/null +++ b/api-examples/operator/ops.tensor_dot.ipynb @@ -0,0 +1,205 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.tensor_dot(x1, x2, axes) -〉 Tensor\n", + "在指定轴上对两个输入进行点乘操作。\n", + "- 输入:\n", + " * x1:mindspore的tensor, 数据类型为float16或float32。\n", + " * x2:mindspore的tensor, 数据类型为float16或float32。\n", + " * axes:int, tuple(int), tuple(tuple(int)), list(list(int))。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| x1 | a | a |\n", + "| x2 | b | b |\n", + "| axes | dims | axes |\n", + "| | out | precision |\n", + "| | | preferred_element_type |\n", + "* torch额外提供了out出参\n", + "* jax额外提供了precision用于设置后端精度,以及preferred_element_type设置返回值的数据类型。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[ 66. 66. 66. 66. 66.]\n", + " [210. 210. 210. 210. 210.]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[ 66., 66., 66., 66., 66.],\n", + " [210., 210., 210., 210., 210.]], dtype=torch.float64)\n", + "\n", + "\n", + "jax output:\n", + " [[ 66. 66. 66. 66. 66.]\n", + " [210. 210. 210. 210. 210.]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "x1 = np.arange(24.).reshape(2, 3, 4)\n", + "x2 = np.ones((3, 4, 5))\n", + "\n", + "y1 = ms.ops.tensor_dot(ms.tensor(x1), ms.tensor(x2), axes=([1, 2], [0, 1]))\n", + "y2 = torch.tensordot(torch.tensor(x1), torch.tensor(x2), dims=([1, 2], [0, 1]))\n", + "y3 = jnp.tensordot(x1, x2, axes=([1, 2], [0, 1]))\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "For primitive[Shape], the input argument must be Tensor, but got .", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensor_dot\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx1\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx2\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxes\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/math_func.py:11460\u001b[0m, in \u001b[0;36mtensor_dot\u001b[0;34m(x1, x2, axes)\u001b[0m\n\u001b[1;32m 11458\u001b[0m matmul_op \u001b[38;5;241m=\u001b[39m _get_cache_prim(P\u001b[38;5;241m.\u001b[39mMatMul)(\u001b[38;5;28;01mFalse\u001b[39;00m, \u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m 11459\u001b[0m \u001b[38;5;66;03m# input validity checks\u001b[39;00m\n\u001b[0;32m> 11460\u001b[0m x1_shape \u001b[38;5;241m=\u001b[39m \u001b[43mshape_\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx1\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 11461\u001b[0m x2_shape \u001b[38;5;241m=\u001b[39m shape_(x2)\n\u001b[1;32m 11462\u001b[0m axes \u001b[38;5;241m=\u001b[39m _check_axes(axes, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtensor_dot\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/operations/manually_defined/ops_def.py:864\u001b[0m, in \u001b[0;36mShape.__call__\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m 862\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(x, (Tensor, COOTensor, CSRTensor, Tensor_)):\n\u001b[1;32m 863\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m x\u001b[38;5;241m.\u001b[39mshape\n\u001b[0;32m--> 864\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFor primitive[\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m], the input argument must be Tensor, but got \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(x)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "\u001b[0;31mTypeError\u001b[0m: For primitive[Shape], the input argument must be Tensor, but got ." + ] + } + ], + "source": [ + "y1 = ms.ops.tensor_dot(x1, x2, axes=([1, 2], [0, 1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "tensordot(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensordot\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx1\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx2\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdims\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/torch/functional.py:1213\u001b[0m, in \u001b[0;36mtensordot\u001b[0;34m(a, b, dims, out)\u001b[0m\n\u001b[1;32m 1210\u001b[0m dims_b \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(\u001b[38;5;28mrange\u001b[39m(dims))\n\u001b[1;32m 1212\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m out \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 1213\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_VF\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensordot\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdims_a\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdims_b\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# type: ignore[attr-defined]\u001b[39;00m\n\u001b[1;32m 1214\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 1215\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _VF\u001b[38;5;241m.\u001b[39mtensordot(a, b, dims_a, dims_b, out\u001b[38;5;241m=\u001b[39mout)\n", + "\u001b[0;31mTypeError\u001b[0m: tensordot(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.tensordot(x1, x2, dims=([1, 2], [0, 1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "tensordot requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensordot\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx2\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxes\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:4879\u001b[0m, in \u001b[0;36mtensordot\u001b[0;34m(a, b, axes, precision, preferred_element_type)\u001b[0m\n\u001b[1;32m 4804\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtensordot\u001b[39m(a: ArrayLike, b: ArrayLike,\n\u001b[1;32m 4805\u001b[0m axes: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m|\u001b[39m Sequence[\u001b[38;5;28mint\u001b[39m] \u001b[38;5;241m|\u001b[39m Sequence[Sequence[\u001b[38;5;28mint\u001b[39m]] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m2\u001b[39m,\n\u001b[1;32m 4806\u001b[0m \u001b[38;5;241m*\u001b[39m, precision: PrecisionLike \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 4807\u001b[0m preferred_element_type: DTypeLike \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[1;32m 4808\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Compute the tensor dot product of two N-dimensional arrays.\u001b[39;00m\n\u001b[1;32m 4809\u001b[0m \n\u001b[1;32m 4810\u001b[0m \u001b[38;5;124;03m JAX implementation of :func:`numpy.linalg.tensordot`.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 4877\u001b[0m \u001b[38;5;124;03m [2, 4, 6]], dtype=int32)\u001b[39;00m\n\u001b[1;32m 4878\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 4879\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtensordot\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4880\u001b[0m dtypes\u001b[38;5;241m.\u001b[39mcheck_user_dtype_supported(preferred_element_type, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtensordot\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 4881\u001b[0m a, b \u001b[38;5;241m=\u001b[39m asarray(a), asarray(b)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: tensordot requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.tensordot(input, x2, axes=([1, 2], [0, 1]))" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.tile.ipynb b/api-examples/operator/ops.tile.ipynb new file mode 100644 index 0000000..dd7301f --- /dev/null +++ b/api-examples/operator/ops.tile.ipynb @@ -0,0 +1,204 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.tile(input, dims) -〉 Tensor\n", + "根据指定维度复制input。\n", + "- 输入:\n", + " * input:mindspore的tensor。\n", + " * dims:tuple[int]。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | a |\n", + "| dims | dims | reps |" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[1 2 1 2 1 2]\n", + " [3 4 3 4 3 4]\n", + " [1 2 1 2 1 2]\n", + " [3 4 3 4 3 4]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[1, 2, 1, 2, 1, 2],\n", + " [3, 4, 3, 4, 3, 4],\n", + " [1, 2, 1, 2, 1, 2],\n", + " [3, 4, 3, 4, 3, 4]])\n", + "\n", + "\n", + "jax output:\n", + " [[1 2 1 2 1 2]\n", + " [3 4 3 4 3 4]\n", + " [1 2 1 2 1 2]\n", + " [3 4 3 4 3 4]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[1, 2],\n", + " [3, 4,]])\n", + "\n", + "y1 = ms.ops.tile(ms.tensor(input), (2, 3))\n", + "y2 = torch.tile(torch.tensor(input), (2, 3))\n", + "y3 = jnp.tile(input, (2, 3))\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling Tile with \"Tile()(input=, dims=Tuple)\".\nThe valid calling should be: \n\"Tile()(input=, dims=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtile\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/operations/manually_defined/ops_def.py:1095\u001b[0m, in \u001b[0;36mtile\u001b[0;34m(input, dims)\u001b[0m\n\u001b[1;32m 1028\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1029\u001b[0m \u001b[38;5;124;03mCreates a new tensor by replicating `input` `dims` times. The i'th dimension of\u001b[39;00m\n\u001b[1;32m 1030\u001b[0m \u001b[38;5;124;03moutput tensor has `input.shape[i] * dims[i]` elements, and the values of `input`\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1092\u001b[0m \u001b[38;5;124;03m [3. 4. 3. 4.]]]\u001b[39;00m\n\u001b[1;32m 1093\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1094\u001b[0m tile_op \u001b[38;5;241m=\u001b[39m _get_cache_prim(Tile)()\n\u001b[0;32m-> 1095\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mtile_op\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdims\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/operations/manually_defined/ops_def.py:1008\u001b[0m, in \u001b[0;36mTile.__call__\u001b[0;34m(self, input, dims)\u001b[0m\n\u001b[1;32m 1007\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;28minput\u001b[39m, dims):\n\u001b[0;32m-> 1008\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_stub(\u001b[43mpyboost_tile\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdims\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m)\n", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Tile with \"Tile()(input=, dims=Tuple)\".\nThe valid calling should be: \n\"Tile()(input=, dims=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.tile(input, (2, 3))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "tile(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtile\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: tile(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.tile(input, (2, 3))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "tile requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtile\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:2859\u001b[0m, in \u001b[0;36mtile\u001b[0;34m(A, reps)\u001b[0m\n\u001b[1;32m 2857\u001b[0m \u001b[38;5;129m@util\u001b[39m\u001b[38;5;241m.\u001b[39mimplements(np\u001b[38;5;241m.\u001b[39mtile)\n\u001b[1;32m 2858\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtile\u001b[39m(A: ArrayLike, reps: DimSize \u001b[38;5;241m|\u001b[39m Sequence[DimSize]) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m-> 2859\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtile\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mA\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2860\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 2861\u001b[0m \u001b[38;5;28miter\u001b[39m(reps) \u001b[38;5;66;03m# type: ignore[arg-type]\u001b[39;00m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: tile requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.tile(input, (2, 3))" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.topk.ipynb b/api-examples/operator/ops.topk.ipynb new file mode 100644 index 0000000..1b6f8d2 --- /dev/null +++ b/api-examples/operator/ops.topk.ipynb @@ -0,0 +1,259 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.topk(input, k, dim=None, largest=True, sorted=True) -〉Tuple(Tensor)\n", + "沿给定维度查找 k 个最大或最小元素和对应的索引。\n", + "- 输入:\n", + " * input: mindspore的tensor。\n", + " * k: int。\n", + " * dim: int。\n", + " * largest: bool。\n", + " * sorted: bool。\n", + "- 返回:由两个mindspore的tensor组成的tuple。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | operand |\n", + "| k | k | k |\n", + "| dim | dim | |\n", + "| largest | largest | |\n", + "| sorted | sorted | |\n", + "| | out | |\n", + "\n", + "* ms与torch基本对齐,torch额外提供了out的出参方式。\n", + "* jax没有提供dim、largest与sorted参数。不能按维度计算、查找最小值及规定返回值的排序。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "markdown", + "id": "620068a6-55c7-4bc6-a6fa-ab9395de5da9", + "metadata": {}, + "source": [ + "一维数组:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output: (Tensor(shape=[3], dtype=Float64, value= [ 5.00000000e+00, 4.00000000e+00, 3.00000000e+00]), Tensor(shape=[3], dtype=Int32, value= [4, 3, 2]))\n", + "torch output: torch.return_types.topk(\n", + "values=tensor([5., 4., 3.], dtype=torch.float64),\n", + "indices=tensor([4, 3, 2]))\n", + "jax output: [Array([5., 4., 3.], dtype=float32), Array([4, 3, 2], dtype=int32)]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.lax as jlx\n", + "\n", + "input = np.array([ 1., 2., 3., 4., 5.])\n", + "\n", + "y1 = ms.ops.topk(ms.tensor(input), 3)\n", + "y2 = torch.topk(torch.tensor(input), 3)\n", + "y3 = jlx.top_k(input, 3)\n", + "print ('mindspore output:',y1)\n", + "print ('torch output:',y2)\n", + "print ('jax output:',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "473b1fde-202f-48d4-9ca1-c687120fefb6", + "metadata": {}, + "source": [ + "二维数组:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "ab0c7d20-410c-451b-8ed8-74efeff201b3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " (Tensor(shape=[2, 4], dtype=Float64, value=\n", + "[[ 5.36800000e-01, 6.52500000e-01, 9.67500000e-01, 9.67300000e-01],\n", + " [ 4.38800000e-01, 5.15200000e-01, 4.68500000e-01, 8.23000000e-01]]), Tensor(shape=[2, 4], dtype=Int32, value=\n", + "[[0, 1, 2, 0],\n", + " [1, 2, 1, 2]]))\n", + "\n", + "\n", + "torch output:\n", + " torch.return_types.topk(\n", + "values=tensor([[0.5368, 0.6525, 0.9675, 0.9673],\n", + " [0.4388, 0.5152, 0.4685, 0.8230]], dtype=torch.float64),\n", + "indices=tensor([[0, 1, 2, 0],\n", + " [1, 2, 1, 2]]))\n" + ] + } + ], + "source": [ + "input = np.array([[0.5368, 0.2447, 0.4302, 0.9673],\n", + " [0.4388, 0.6525, 0.4685, 0.1868],\n", + " [0.3563, 0.5152, 0.9675, 0.8230]])\n", + "\n", + "y1 = ms.ops.topk(ms.tensor(input), 2, dim=0)\n", + "y2 = torch.topk(torch.tensor(input), 2, dim=0)\n", + "print ('mindspore output:\\n',y1)\n", + "print ('\\n')\n", + "print ('torch output:\\n',y2)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* m与jax都不返回类型" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "For Primitive[TopK], the type of input argument[input_x] must be Tensor but got External.\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/core/utils/check_convert_utils.cc:880 CheckTensorTypeValid\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtopk\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/array_func.py:5806\u001b[0m, in \u001b[0;36mtopk\u001b[0;34m(input, k, dim, largest, sorted)\u001b[0m\n\u001b[1;32m 5804\u001b[0m values, indices \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m-\u001b[39mres[\u001b[38;5;241m0\u001b[39m], res[\u001b[38;5;241m1\u001b[39m]\n\u001b[1;32m 5805\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m values, indices\n\u001b[0;32m-> 5806\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mtop_k_\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mk\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 5807\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28minput\u001b[39m\u001b[38;5;241m.\u001b[39mswapaxes(dim, \u001b[38;5;28minput\u001b[39m\u001b[38;5;241m.\u001b[39mndim \u001b[38;5;241m-\u001b[39m \u001b[38;5;241m1\u001b[39m)\n\u001b[1;32m 5808\u001b[0m output \u001b[38;5;241m=\u001b[39m top_k_(\u001b[38;5;28minput\u001b[39m, k)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:405\u001b[0m, in \u001b[0;36mPrimitive.__call__\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 403\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m should_elim:\n\u001b[1;32m 404\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m output\n\u001b[0;32m--> 405\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_run_op\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:1022\u001b[0m, in \u001b[0;36m_run_op\u001b[0;34m(obj, op_name, args)\u001b[0m\n\u001b[1;32m 1020\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Single op execution function supported by ge in PyNative mode.\"\"\"\u001b[39;00m\n\u001b[1;32m 1021\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent:\n\u001b[0;32m-> 1022\u001b[0m stub \u001b[38;5;241m=\u001b[39m \u001b[43m_pynative_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1023\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_stub(stub)\n\u001b[1;32m 1024\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent\u001b[38;5;241m.\u001b[39mhook(obj, args)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/common/api.py:1423\u001b[0m, in \u001b[0;36m_PyNativeExecutor.run_op_async\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 1413\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrun_op_async\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs):\n\u001b[1;32m 1414\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1415\u001b[0m \u001b[38;5;124;03m Run single op async.\u001b[39;00m\n\u001b[1;32m 1416\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1421\u001b[0m \u001b[38;5;124;03m StubNode, result of run op.\u001b[39;00m\n\u001b[1;32m 1422\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1423\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: For Primitive[TopK], the type of input argument[input_x] must be Tensor but got External.\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/core/utils/check_convert_utils.cc:880 CheckTensorTypeValid\n" + ] + } + ], + "source": [ + "y1 = ms.ops.topk(input, 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "topk(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtopk\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: topk(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.topk(input, 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Value [Tracedwith, Tracedwith, Tracedwith, Tracedwith] with type is not a valid JAX type", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[11], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjlx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtop_k\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/lax/lax.py:1258\u001b[0m, in \u001b[0;36mtop_k\u001b[0;34m(operand, k)\u001b[0m\n\u001b[1;32m 1256\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m k \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 1257\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mk argument to top_k must be nonnegative, got \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mk\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m-> 1258\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mtop_k_p\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbind\u001b[49m\u001b[43m(\u001b[49m\u001b[43moperand\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mk\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mk\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/core.py:416\u001b[0m, in \u001b[0;36mPrimitive.bind\u001b[0;34m(self, *args, **params)\u001b[0m\n\u001b[1;32m 413\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mbind\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mparams):\n\u001b[1;32m 414\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m (\u001b[38;5;129;01mnot\u001b[39;00m config\u001b[38;5;241m.\u001b[39menable_checks\u001b[38;5;241m.\u001b[39mvalue \u001b[38;5;129;01mor\u001b[39;00m\n\u001b[1;32m 415\u001b[0m \u001b[38;5;28mall\u001b[39m(\u001b[38;5;28misinstance\u001b[39m(arg, Tracer) \u001b[38;5;129;01mor\u001b[39;00m valid_jaxtype(arg) \u001b[38;5;28;01mfor\u001b[39;00m arg \u001b[38;5;129;01min\u001b[39;00m args)), args\n\u001b[0;32m--> 416\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbind_with_trace\u001b[49m\u001b[43m(\u001b[49m\u001b[43mfind_top_trace\u001b[49m\u001b[43m(\u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparams\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/core.py:420\u001b[0m, in \u001b[0;36mPrimitive.bind_with_trace\u001b[0;34m(self, trace, args, params)\u001b[0m\n\u001b[1;32m 418\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mbind_with_trace\u001b[39m(\u001b[38;5;28mself\u001b[39m, trace, args, params):\n\u001b[1;32m 419\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m pop_level(trace\u001b[38;5;241m.\u001b[39mlevel):\n\u001b[0;32m--> 420\u001b[0m out \u001b[38;5;241m=\u001b[39m \u001b[43mtrace\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mprocess_primitive\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mmap\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mtrace\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfull_raise\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparams\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 421\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mmap\u001b[39m(full_lower, out) \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mmultiple_results \u001b[38;5;28;01melse\u001b[39;00m full_lower(out)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/core.py:921\u001b[0m, in \u001b[0;36mEvalTrace.process_primitive\u001b[0;34m(self, primitive, tracers, params)\u001b[0m\n\u001b[1;32m 919\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m call_impl_with_key_reuse_checks(primitive, primitive\u001b[38;5;241m.\u001b[39mimpl, \u001b[38;5;241m*\u001b[39mtracers, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mparams)\n\u001b[1;32m 920\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 921\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mprimitive\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mimpl\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mtracers\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mparams\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/dispatch.py:87\u001b[0m, in \u001b[0;36mapply_primitive\u001b[0;34m(prim, *args, **params)\u001b[0m\n\u001b[1;32m 85\u001b[0m prev \u001b[38;5;241m=\u001b[39m lib\u001b[38;5;241m.\u001b[39mjax_jit\u001b[38;5;241m.\u001b[39mswap_thread_local_state_disable_jit(\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m 86\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 87\u001b[0m outs \u001b[38;5;241m=\u001b[39m \u001b[43mfun\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 88\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m 89\u001b[0m lib\u001b[38;5;241m.\u001b[39mjax_jit\u001b[38;5;241m.\u001b[39mswap_thread_local_state_disable_jit(prev)\n", + " \u001b[0;31m[... skipping hidden 17 frame]\u001b[0m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/core.py:1497\u001b[0m, in \u001b[0;36mconcrete_aval\u001b[0;34m(x)\u001b[0m\n\u001b[1;32m 1495\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(x, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m__jax_array__\u001b[39m\u001b[38;5;124m'\u001b[39m):\n\u001b[1;32m 1496\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m concrete_aval(x\u001b[38;5;241m.\u001b[39m__jax_array__())\n\u001b[0;32m-> 1497\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mValue \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mx\u001b[38;5;132;01m!r}\u001b[39;00m\u001b[38;5;124m with type \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(x)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m is not a valid JAX \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1498\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtype\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "\u001b[0;31mTypeError\u001b[0m: Value [Tracedwith, Tracedwith, Tracedwith, Tracedwith] with type is not a valid JAX type" + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jlx.top_k(input, 1)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.trace.ipynb b/api-examples/operator/ops.trace.ipynb new file mode 100644 index 0000000..3805045 --- /dev/null +++ b/api-examples/operator/ops.trace.ipynb @@ -0,0 +1,287 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.trace(input) -〉 Tensor\n", + "返回二维输入在主对角线方向上的元素总和。\n", + "- 输入:input必须是mindspore的二维tensor, 数据类型不支持复数。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | a |\n", + "| | | offset |\n", + "| | | axis1 |\n", + "| | | axis2 |\n", + "| | | dtype |\n", + "| | | out (unused)|\n", + "\n", + "* jax额外提供了offest、axis1、axis2、dtype参数,分别用于设置对角线偏移量、沿哪个维度进行求和以及返回值的数据类型。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " 5\n", + "\n", + "\n", + "torch output:\n", + " tensor(5)\n", + "\n", + "\n", + "jax output:\n", + " 5\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[1, 2],\n", + " [3, 4],\n", + " [5, 6],\n", + " [7, 8]])\n", + "\n", + "y1 = ms.ops.trace(ms.tensor(input))\n", + "y2 = torch.trace(torch.tensor(input))\n", + "y3 = jnp.trace(input)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "facce0be-8df1-4f7e-ae03-dd415593fc09", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "96d7ece4-5960-4779-9eea-b53e69870f11", + "metadata": {}, + "source": [ + "jax使用offest、axis1、axis2参数:" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "5cdb36e1-c408-437d-ab60-d3a5d41c64ce", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "jax output:\n", + " [ 8 10]\n" + ] + } + ], + "source": [ + "input = np.array([[[1, 2],\n", + " [3, 4]],\n", + "\n", + " [[5, 6],\n", + " [7, 8]]])\n", + "\n", + "y3 = jnp.trace(input)\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "a2a98229-38cd-436c-99c4-827af7e0497c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "jax output:\n", + " [3 4]\n" + ] + } + ], + "source": [ + "y3 = jnp.trace(input, offset = 1)\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "9c14824e-0c50-4fad-8012-cc9b9a75af47", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "jax output:\n", + " [ 5 13]\n" + ] + } + ], + "source": [ + "y3 = jnp.trace(input, axis1=1, axis2=2)\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "418f04b8-e7c0-4cae-8d5d-39e55697e020", + "metadata": {}, + "source": [ + "* ms与torch仅能计算二维数组,jax可以计算多维数组。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling Trace with \"Trace()(input=)\".\nThe valid calling should be: \n\"Trace()(input=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[18], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtrace\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_def.py:7969\u001b[0m, in \u001b[0;36mtrace\u001b[0;34m(input)\u001b[0m\n\u001b[1;32m 7932\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtrace\u001b[39m(\u001b[38;5;28minput\u001b[39m):\n\u001b[1;32m 7933\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 7934\u001b[0m \u001b[38;5;124;03m Returns a new tensor that is the sum of the `input` main trace.\u001b[39;00m\n\u001b[1;32m 7935\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 7967\u001b[0m \u001b[38;5;124;03m 24.0\u001b[39;00m\n\u001b[1;32m 7968\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 7969\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mtrace_op\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py:15046\u001b[0m, in \u001b[0;36mTrace.__call__\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 15045\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;28minput\u001b[39m):\n\u001b[0;32m> 15046\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__call__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:405\u001b[0m, in \u001b[0;36mPrimitive.__call__\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 403\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m should_elim:\n\u001b[1;32m 404\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m output\n\u001b[0;32m--> 405\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_run_op\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:1022\u001b[0m, in \u001b[0;36m_run_op\u001b[0;34m(obj, op_name, args)\u001b[0m\n\u001b[1;32m 1020\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Single op execution function supported by ge in PyNative mode.\"\"\"\u001b[39;00m\n\u001b[1;32m 1021\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent:\n\u001b[0;32m-> 1022\u001b[0m stub \u001b[38;5;241m=\u001b[39m \u001b[43m_pynative_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1023\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_stub(stub)\n\u001b[1;32m 1024\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent\u001b[38;5;241m.\u001b[39mhook(obj, args)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/common/api.py:1423\u001b[0m, in \u001b[0;36m_PyNativeExecutor.run_op_async\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 1413\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrun_op_async\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs):\n\u001b[1;32m 1414\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1415\u001b[0m \u001b[38;5;124;03m Run single op async.\u001b[39;00m\n\u001b[1;32m 1416\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1421\u001b[0m \u001b[38;5;124;03m StubNode, result of run op.\u001b[39;00m\n\u001b[1;32m 1422\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1423\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Trace with \"Trace()(input=)\".\nThe valid calling should be: \n\"Trace()(input=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.trace(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "trace(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[19], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtrace\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: trace(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.trace(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "trace requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[20], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtrace\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + " \u001b[0;31m[... skipping hidden 11 frame]\u001b[0m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:4021\u001b[0m, in \u001b[0;36mtrace\u001b[0;34m(a, offset, axis1, axis2, dtype, out)\u001b[0m\n\u001b[1;32m 4017\u001b[0m \u001b[38;5;129m@util\u001b[39m\u001b[38;5;241m.\u001b[39mimplements(np\u001b[38;5;241m.\u001b[39mtrace, skip_params\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mout\u001b[39m\u001b[38;5;124m'\u001b[39m])\n\u001b[1;32m 4018\u001b[0m \u001b[38;5;129m@partial\u001b[39m(jit, static_argnames\u001b[38;5;241m=\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124maxis1\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124maxis2\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdtype\u001b[39m\u001b[38;5;124m'\u001b[39m))\n\u001b[1;32m 4019\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtrace\u001b[39m(a: ArrayLike, offset: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m|\u001b[39m ArrayLike \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m, axis1: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m, axis2: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m,\n\u001b[1;32m 4020\u001b[0m dtype: DTypeLike \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m, out: \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m-> 4021\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtrace\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4022\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m out \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 4023\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mout\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m argument to jnp.trace is not supported.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: trace requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.trace(input)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.transpose.ipynb b/api-examples/operator/ops.transpose.ipynb new file mode 100644 index 0000000..a0ee4eb --- /dev/null +++ b/api-examples/operator/ops.transpose.ipynb @@ -0,0 +1,268 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.transpose(input, input_perm) -〉 Tensor\n", + "按指定的排列对输入进行数据重排。\n", + "- 输入:\n", + " * input:mindspore的tensor。\n", + " * input_perm:tuple(int)。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | a |\n", + "| input_perm | dim0 | axes |\n", + "| | dim1 | |\n", + "\n", + "* ms与jax相同,使用第二个参数来设置按什么维度顺序进行重排。torch使用第二、三个参数设置按哪个维度进行重排。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "markdown", + "id": "6bd9bdf0-bc8b-4fe3-9e2f-c9e032f19f67", + "metadata": {}, + "source": [ + "未设定重排维度情况下,默认值比较:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "jax output:\n", + " [[[ 1 7]\n", + " [ 4 10]]\n", + "\n", + " [[ 2 8]\n", + " [ 5 11]]\n", + "\n", + " [[ 3 9]\n", + " [ 6 12]]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[[1, 2, 3], \n", + " [4, 5, 6]], \n", + " [[7, 8, 9], \n", + " [10, 11, 12]]])\n", + "\n", + "# y1 = ms.ops.transpose(ms.tensor(input))\n", + "# y2 = torch.transpose(torch.tensor(input))\n", + "y3 = jnp.transpose(input)\n", + "# print ('mindspore output:\\n',y1)\n", + "# print('\\n')\n", + "# print ('torch output:\\n',y2)\n", + "# print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "7e425850-50eb-4ac9-8f02-728ea64225a7", + "metadata": {}, + "source": [ + "* ms与torch的input_perm、dim0和dim1均为必选参数必须设置。\n", + "* jax的默认行为是按将整个维度倒序排列,如上述例子,等同于设置axes=(2, 1, 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "50e41829-6783-4032-bd5d-867b97b1dc18", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[[ 1 4]\n", + " [ 2 5]\n", + " [ 3 6]]\n", + "\n", + " [[ 7 10]\n", + " [ 8 11]\n", + " [ 9 12]]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[[ 1, 4],\n", + " [ 2, 5],\n", + " [ 3, 6]],\n", + "\n", + " [[ 7, 10],\n", + " [ 8, 11],\n", + " [ 9, 12]]])\n", + "\n", + "\n", + "jax output:\n", + " [[[ 1 4]\n", + " [ 2 5]\n", + " [ 3 6]]\n", + "\n", + " [[ 7 10]\n", + " [ 8 11]\n", + " [ 9 12]]]\n" + ] + } + ], + "source": [ + "y1 = ms.ops.transpose(ms.tensor(input), (0, 2, 1))\n", + "y2 = torch.transpose(torch.tensor(input), dim0=2, dim1=1)\n", + "y3 = jnp.transpose(input, (0, 2, 1))\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling Transpose with \"Transpose()(input=, input_perm=Tuple)\".\nThe valid calling should be: \n\"Transpose()(input=, input_perm=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/wy/5yzfdb6x7pvfxh89zcmf_w780000gn/T/ipykernel_18001/1707311972.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0my1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtranspose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_def.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(input, input_perm)\u001b[0m\n\u001b[1;32m 8051\u001b[0m [[ 7. 10.]\n\u001b[1;32m 8052\u001b[0m \u001b[0;34m[\u001b[0m \u001b[0;36m8.\u001b[0m \u001b[0;36m11.\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8053\u001b[0m [ 9. 12.]]]\n\u001b[1;32m 8054\u001b[0m \"\"\"\n\u001b[0;32m-> 8055\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mtranspose_op\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput_perm\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(self, input, input_perm)\u001b[0m\n\u001b[1;32m 15142\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput_perm\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m> 15143\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_convert_stub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpyboost_transpose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput_perm\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Transpose with \"Transpose()(input=, input_perm=Tuple)\".\nThe valid calling should be: \n\"Transpose()(input=, input_perm=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.transpose(input, (0, 2, 1))" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "transpose() received an invalid combination of arguments - got (numpy.ndarray, tuple), but expected one of:\n * (Tensor input, int dim0, int dim1)\n * (Tensor input, name dim0, name dim1)\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[14], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtranspose\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: transpose() received an invalid combination of arguments - got (numpy.ndarray, tuple), but expected one of:\n * (Tensor input, int dim0, int dim1)\n * (Tensor input, name dim0, name dim1)\n" + ] + } + ], + "source": [ + "y2 = torch.transpose(input, (0, 2, 1))" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "transpose requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[15], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtranspose\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:765\u001b[0m, in \u001b[0;36mtranspose\u001b[0;34m(a, axes)\u001b[0m\n\u001b[1;32m 693\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtranspose\u001b[39m(a: ArrayLike, axes: Sequence[\u001b[38;5;28mint\u001b[39m] \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[1;32m 694\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Return a transposed version of an N-dimensional array.\u001b[39;00m\n\u001b[1;32m 695\u001b[0m \n\u001b[1;32m 696\u001b[0m \u001b[38;5;124;03m JAX implementation of :func:`numpy.transpose`, implemented in terms of\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 763\u001b[0m \u001b[38;5;124;03m [2, 4]], dtype=int32)\u001b[39;00m\n\u001b[1;32m 764\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 765\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtranspose\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 766\u001b[0m axes_ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(\u001b[38;5;28mrange\u001b[39m(ndim(a))[::\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m]) \u001b[38;5;28;01mif\u001b[39;00m axes \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;28;01melse\u001b[39;00m axes\n\u001b[1;32m 767\u001b[0m axes_ \u001b[38;5;241m=\u001b[39m [_canonicalize_axis(i, ndim(a)) \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m axes_]\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: transpose requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.transpose(input, (0, 2, 1))" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.tril.ipynb b/api-examples/operator/ops.tril.ipynb new file mode 100644 index 0000000..06ead6e --- /dev/null +++ b/api-examples/operator/ops.tril.ipynb @@ -0,0 +1,320 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.tril(input, diagonal=0) -〉 Tensor\n", + "返回输入的对角线和下面的元素,并将其他元素设置为0。\n", + "- 输入:\n", + " * input:mindspore的tensor。\n", + " * diagonal:int。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | m |\n", + "| diagonal | diagonal | k |\n", + "| | out | |\n", + "\n", + "* torch额外提供了out出参。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "markdown", + "id": "af91e17e-2245-445b-affa-2a6a5fd49645", + "metadata": {}, + "source": [ + "二维输入下diagonal默认值比较:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[ 1 0 0 0]\n", + " [ 5 6 0 0]\n", + " [ 9 10 11 0]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[ 1, 0, 0, 0],\n", + " [ 5, 6, 0, 0],\n", + " [ 9, 10, 11, 0]])\n", + "\n", + "\n", + "jax output:\n", + " [[ 1 0 0 0]\n", + " [ 5 6 0 0]\n", + " [ 9 10 11 0]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[1, 2, 3, 4],\n", + " [5, 6, 7, 8],\n", + " [9, 10, 11, 12]])\n", + "\n", + "y1 = ms.ops.tril(ms.tensor(input))\n", + "y2 = torch.tril(torch.tensor(input))\n", + "y3 = jnp.tril(input)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "2166027e-0c9f-43ec-b33b-9655dc670f23", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[ 1 2 0 0]\n", + " [ 5 6 7 0]\n", + " [ 9 10 11 12]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[ 1, 2, 0, 0],\n", + " [ 5, 6, 7, 0],\n", + " [ 9, 10, 11, 12]])\n", + "\n", + "\n", + "jax output:\n", + " [[ 1 2 0 0]\n", + " [ 5 6 7 0]\n", + " [ 9 10 11 12]]\n" + ] + } + ], + "source": [ + "y1 = ms.ops.tril(ms.tensor(input), 1)\n", + "y2 = torch.tril(torch.tensor(input), 1)\n", + "y3 = jnp.tril(input, 1)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "59f6d554-53e4-49ed-a5ca-0a7fd20ba7a6", + "metadata": {}, + "source": [ + "三维输入:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "50e41829-6783-4032-bd5d-867b97b1dc18", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[[1 0]\n", + " [3 4]]\n", + "\n", + " [[5 0]\n", + " [7 8]]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[[1, 0],\n", + " [3, 4]],\n", + "\n", + " [[5, 0],\n", + " [7, 8]]])\n", + "\n", + "\n", + "jax output:\n", + " [[[1 0]\n", + " [3 4]]\n", + "\n", + " [[5 0]\n", + " [7 8]]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[[1, 2],\n", + " [3, 4]],\n", + " [[5, 6],\n", + " [7, 8]]])\n", + "\n", + "y1 = ms.ops.tril(ms.tensor(input))\n", + "y2 = torch.tril(torch.tensor(input))\n", + "y3 = jnp.tril(input)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "For Primitive[Tril], the type of input argument[x] must be Tensor but got External.\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/core/utils/check_convert_utils.cc:880 CheckTensorTypeValid\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtril\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/array_func.py:4990\u001b[0m, in \u001b[0;36mtril\u001b[0;34m(input, diagonal)\u001b[0m\n\u001b[1;32m 4933\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 4934\u001b[0m \u001b[38;5;124;03mReturns the lower triangle part of `input` (elements that contain the diagonal and below),\u001b[39;00m\n\u001b[1;32m 4935\u001b[0m \u001b[38;5;124;03mand set the other elements to zeros.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 4987\u001b[0m \u001b[38;5;124;03m [14 15 16 0]]\u001b[39;00m\n\u001b[1;32m 4988\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 4989\u001b[0m tril_ \u001b[38;5;241m=\u001b[39m _get_cache_prim(Tril)(diagonal)\n\u001b[0;32m-> 4990\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mtril_\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:405\u001b[0m, in \u001b[0;36mPrimitive.__call__\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 403\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m should_elim:\n\u001b[1;32m 404\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m output\n\u001b[0;32m--> 405\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_run_op\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/primitive.py:1022\u001b[0m, in \u001b[0;36m_run_op\u001b[0;34m(obj, op_name, args)\u001b[0m\n\u001b[1;32m 1020\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Single op execution function supported by ge in PyNative mode.\"\"\"\u001b[39;00m\n\u001b[1;32m 1021\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent:\n\u001b[0;32m-> 1022\u001b[0m stub \u001b[38;5;241m=\u001b[39m \u001b[43m_pynative_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[43mobj\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mop_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1023\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_stub(stub)\n\u001b[1;32m 1024\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _RunOpHook\u001b[38;5;241m.\u001b[39mcurrent\u001b[38;5;241m.\u001b[39mhook(obj, args)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/common/api.py:1423\u001b[0m, in \u001b[0;36m_PyNativeExecutor.run_op_async\u001b[0;34m(self, *args)\u001b[0m\n\u001b[1;32m 1413\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrun_op_async\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39margs):\n\u001b[1;32m 1414\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1415\u001b[0m \u001b[38;5;124;03m Run single op async.\u001b[39;00m\n\u001b[1;32m 1416\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1421\u001b[0m \u001b[38;5;124;03m StubNode, result of run op.\u001b[39;00m\n\u001b[1;32m 1422\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1423\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_executor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrun_op_async\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: For Primitive[Tril], the type of input argument[x] must be Tensor but got External.\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/core/utils/check_convert_utils.cc:880 CheckTensorTypeValid\n" + ] + } + ], + "source": [ + "y1 = ms.ops.tril(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "tril(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtril\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: tril(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.tril(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "tril requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[9], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtril\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + " \u001b[0;31m[... skipping hidden 11 frame]\u001b[0m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:3996\u001b[0m, in \u001b[0;36mtril\u001b[0;34m(m, k)\u001b[0m\n\u001b[1;32m 3993\u001b[0m \u001b[38;5;129m@util\u001b[39m\u001b[38;5;241m.\u001b[39mimplements(np\u001b[38;5;241m.\u001b[39mtril)\n\u001b[1;32m 3994\u001b[0m \u001b[38;5;129m@partial\u001b[39m(jit, static_argnames\u001b[38;5;241m=\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mk\u001b[39m\u001b[38;5;124m'\u001b[39m,))\n\u001b[1;32m 3995\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtril\u001b[39m(m: ArrayLike, k: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m-> 3996\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtril\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mm\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3997\u001b[0m m_shape \u001b[38;5;241m=\u001b[39m shape(m)\n\u001b[1;32m 3998\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(m_shape) \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m2\u001b[39m:\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: tril requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.tril(input)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.triu.ipynb b/api-examples/operator/ops.triu.ipynb new file mode 100644 index 0000000..0adfe9c --- /dev/null +++ b/api-examples/operator/ops.triu.ipynb @@ -0,0 +1,318 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.triu(input, diagonal=0) -〉 Tensor\n", + "返回输入的对角线和上面的元素,并将其他元素设置为0。\n", + "- 输入:\n", + " * input:mindspore的tensor。\n", + " * diagonal:int。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | m |\n", + "| diagonal | diagonal | k |\n", + "| | out | |\n", + "\n", + "* torch额外提供了out出参。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "markdown", + "id": "af91e17e-2245-445b-affa-2a6a5fd49645", + "metadata": {}, + "source": [ + "二维输入下diagonal默认值比较:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[ 1 2 3 4]\n", + " [ 0 6 7 8]\n", + " [ 0 0 11 12]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[ 1, 2, 3, 4],\n", + " [ 0, 6, 7, 8],\n", + " [ 0, 0, 11, 12]])\n", + "\n", + "\n", + "jax output:\n", + " [[ 1 2 3 4]\n", + " [ 0 6 7 8]\n", + " [ 0 0 11 12]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[1, 2, 3, 4],\n", + " [5, 6, 7, 8],\n", + " [9, 10, 11, 12]])\n", + "\n", + "y1 = ms.ops.triu(ms.tensor(input))\n", + "y2 = torch.triu(torch.tensor(input))\n", + "y3 = jnp.triu(input)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "2166027e-0c9f-43ec-b33b-9655dc670f23", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[ 0 2 3 4]\n", + " [ 0 0 7 8]\n", + " [ 0 0 0 12]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[ 0, 2, 3, 4],\n", + " [ 0, 0, 7, 8],\n", + " [ 0, 0, 0, 12]])\n", + "\n", + "\n", + "jax output:\n", + " [[ 0 2 3 4]\n", + " [ 0 0 7 8]\n", + " [ 0 0 0 12]]\n" + ] + } + ], + "source": [ + "y1 = ms.ops.triu(ms.tensor(input), 1)\n", + "y2 = torch.triu(torch.tensor(input), 1)\n", + "y3 = jnp.triu(input, 1)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "59f6d554-53e4-49ed-a5ca-0a7fd20ba7a6", + "metadata": {}, + "source": [ + "三维输入:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "50e41829-6783-4032-bd5d-867b97b1dc18", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[[1 2]\n", + " [0 4]]\n", + "\n", + " [[5 6]\n", + " [0 8]]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[[1, 2],\n", + " [0, 4]],\n", + "\n", + " [[5, 6],\n", + " [0, 8]]])\n", + "\n", + "\n", + "jax output:\n", + " [[[1 2]\n", + " [0 4]]\n", + "\n", + " [[5 6]\n", + " [0 8]]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[[1, 2],\n", + " [3, 4]],\n", + " [[5, 6],\n", + " [7, 8]]])\n", + "\n", + "y1 = ms.ops.triu(ms.tensor(input))\n", + "y2 = torch.triu(torch.tensor(input))\n", + "y3 = jnp.triu(input)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling Triu with \"Triu(diagonal=int)(input=)\".\nThe valid calling should be: \n\"Triu(diagonal=)(input=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtriu\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_def.py:8123\u001b[0m, in \u001b[0;36mtriu\u001b[0;34m(input, diagonal)\u001b[0m\n\u001b[1;32m 8065\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtriu\u001b[39m(\u001b[38;5;28minput\u001b[39m, diagonal\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m):\n\u001b[1;32m 8066\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 8067\u001b[0m \u001b[38;5;124;03m Returns the upper triangle part of `input` (elements that contain the diagonal and below),\u001b[39;00m\n\u001b[1;32m 8068\u001b[0m \u001b[38;5;124;03m and set the other elements to zeros.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 8121\u001b[0m \u001b[38;5;124;03m [ 0 0 16 17]]\u001b[39;00m\n\u001b[1;32m 8122\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 8123\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mtriu_impl\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdiagonal\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/pyboost_inner_prim.py:501\u001b[0m, in \u001b[0;36m_PyboostTriuPrim.__call__\u001b[0;34m(self, input, diagonal)\u001b[0m\n\u001b[1;32m 499\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;28minput\u001b[39m, diagonal):\n\u001b[0;32m--> 501\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_stub(\u001b[38;5;28;43msuper\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__call__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdiagonal\u001b[49m\u001b[43m)\u001b[49m)\n", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Triu with \"Triu(diagonal=int)(input=)\".\nThe valid calling should be: \n\"Triu(diagonal=)(input=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.triu(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "triu(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtriu\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: triu(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.triu(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "triu requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[7], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtriu\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + " \u001b[0;31m[... skipping hidden 11 frame]\u001b[0m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:4008\u001b[0m, in \u001b[0;36mtriu\u001b[0;34m(m, k)\u001b[0m\n\u001b[1;32m 4005\u001b[0m \u001b[38;5;129m@util\u001b[39m\u001b[38;5;241m.\u001b[39mimplements(np\u001b[38;5;241m.\u001b[39mtriu, update_doc\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m 4006\u001b[0m \u001b[38;5;129m@partial\u001b[39m(jit, static_argnames\u001b[38;5;241m=\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mk\u001b[39m\u001b[38;5;124m'\u001b[39m,))\n\u001b[1;32m 4007\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtriu\u001b[39m(m: ArrayLike, k: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m-> 4008\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtriu\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mm\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4009\u001b[0m m_shape \u001b[38;5;241m=\u001b[39m shape(m)\n\u001b[1;32m 4010\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(m_shape) \u001b[38;5;241m<\u001b[39m \u001b[38;5;241m2\u001b[39m:\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: triu requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.triu(input)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.var.ipynb b/api-examples/operator/ops.var.ipynb new file mode 100644 index 0000000..1737a9b --- /dev/null +++ b/api-examples/operator/ops.var.ipynb @@ -0,0 +1,250 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.var(input, axis=None, ddof=0, keepdims=False) -〉 Tensor\n", + "按指定维度返回输入Tensor上的方差。\n", + "- 输入:\n", + " * input:mindspore的tensor(数据类型为Number)。\n", + " * axis:int, tuple(int)。\n", + " * ddof:int, bool。\n", + " * keepdims:bool。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | a |\n", + "| axis | dim | axis |\n", + "| ddof | correction | dtype |\n", + "| keepdims | keepdim | out (unused)|\n", + "| | out | ddof |\n", + "| | | keepdims |\n", + "| | | where |\n", + "| | | correction |\n", + "\n", + "* jax额外提供了dtype、where和correction参数,分别用于设置返回值的数据类型、数组位置上的元素是否参与计算方差。\n", + "* torch额外提供了out出参。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " 5.743055083152285\n", + "\n", + "\n", + "torch output:\n", + " tensor(6.2652, dtype=torch.float64)\n", + "\n", + "\n", + "jax output:\n", + " 5.7430553\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[1., 3, 4, 2],\n", + " [5, 2, 6, 3],\n", + " [8, 4, 2, 9]])\n", + "\n", + "y1 = ms.ops.var(ms.tensor(input))\n", + "y2 = torch.var(torch.tensor(input))\n", + "y3 = jnp.var(input)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "50e41829-6783-4032-bd5d-867b97b1dc18", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[ 1.66666656]\n", + " [ 3.33333344]\n", + " [10.91666742]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[ 1.6667],\n", + " [ 3.3333],\n", + " [10.9167]], dtype=torch.float64)\n", + "\n", + "\n", + "jax output:\n", + " [[ 1.6666666]\n", + " [ 3.3333333]\n", + " [10.916667 ]]\n" + ] + } + ], + "source": [ + "y1 = ms.ops.var(ms.tensor(input), 1, keepdims=True, ddof=1)\n", + "y2 = torch.var(torch.tensor(input), 1, correction=1, keepdim=True)\n", + "y3 = jnp.var(input, 1, keepdims=True, ddof=1)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "For var, input must be a Tensor, but got .", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/wy/5yzfdb6x7pvfxh89zcmf_w780000gn/T/ipykernel_8417/2024296880.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0my1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvar\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/math_func.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(input, axis, ddof, keepdims)\u001b[0m\n\u001b[1;32m 4426\u001b[0m \u001b[0;34m>>\u001b[0m\u001b[0;34m>\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4427\u001b[0m [[ 2.5]\n\u001b[1;32m 4428\u001b[0m [54.5]]\n\u001b[1;32m 4429\u001b[0m \"\"\"\n\u001b[0;32m-> 4430\u001b[0;31m \u001b[0maxis\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_check_var_std_input\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mddof\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"var\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4431\u001b[0m \u001b[0moutput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mvar_mean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mddof\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4432\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0moutput\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/math_func.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(input, ddof, keepdims, axis, cls_name)\u001b[0m\n\u001b[1;32m 4310\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_check_var_std_input\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mddof\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcls_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 4311\u001b[0;31m \u001b[0m_check_is_tensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"input\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcls_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4312\u001b[0m \u001b[0m_check_attr_dtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"ddof\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mddof\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbool\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcls_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4313\u001b[0m \u001b[0m_check_attr_dtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"keepdims\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mbool\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcls_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4314\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0maxis\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/math_func.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(param_name, input, cls_name)\u001b[0m\n\u001b[1;32m 9014\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_check_is_tensor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mparam_name\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcls_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9015\u001b[0m \u001b[0;34m\"\"\"Returns True if input is Tensor.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9016\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mTensor\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 9017\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"For {cls_name}, {param_name} must be a Tensor, but got {type(input)}.\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: For var, input must be a Tensor, but got ." + ] + } + ], + "source": [ + "y1 = ms.ops.var(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "var() received an invalid combination of arguments - got (numpy.ndarray), but expected one of:\n * (Tensor input, tuple of ints dim, bool unbiased, bool keepdim, *, Tensor out)\n * (Tensor input, tuple of ints dim, *, Number correction, bool keepdim, Tensor out)\n * (Tensor input, bool unbiased)\n * (Tensor input, tuple of names dim, bool unbiased, bool keepdim, *, Tensor out)\n * (Tensor input, tuple of names dim, *, Number correction, bool keepdim, Tensor out)\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvar\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: var() received an invalid combination of arguments - got (numpy.ndarray), but expected one of:\n * (Tensor input, tuple of ints dim, bool unbiased, bool keepdim, *, Tensor out)\n * (Tensor input, tuple of ints dim, *, Number correction, bool keepdim, Tensor out)\n * (Tensor input, bool unbiased)\n * (Tensor input, tuple of names dim, bool unbiased, bool keepdim, *, Tensor out)\n * (Tensor input, tuple of names dim, *, Number correction, bool keepdim, Tensor out)\n" + ] + } + ], + "source": [ + "y2 = torch.var(input)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "var requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[12], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mvar\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/reductions.py:441\u001b[0m, in \u001b[0;36mvar\u001b[0;34m(a, axis, dtype, out, ddof, keepdims, where, correction)\u001b[0m\n\u001b[1;32m 439\u001b[0m \u001b[38;5;28;01melif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(ddof, \u001b[38;5;28mint\u001b[39m) \u001b[38;5;129;01mor\u001b[39;00m ddof \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[1;32m 440\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mddof and correction can\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mt be provided simultaneously.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 441\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_var\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m_ensure_optional_axes\u001b[49m\u001b[43m(\u001b[49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mout\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcorrection\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdims\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 442\u001b[0m \u001b[43m \u001b[49m\u001b[43mwhere\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwhere\u001b[49m\u001b[43m)\u001b[49m\n", + " \u001b[0;31m[... skipping hidden 11 frame]\u001b[0m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/reductions.py:448\u001b[0m, in \u001b[0;36m_var\u001b[0;34m(a, axis, dtype, out, correction, keepdims, where)\u001b[0m\n\u001b[1;32m 444\u001b[0m \u001b[38;5;129m@partial\u001b[39m(api\u001b[38;5;241m.\u001b[39mjit, static_argnames\u001b[38;5;241m=\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124maxis\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdtype\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mkeepdims\u001b[39m\u001b[38;5;124m'\u001b[39m))\n\u001b[1;32m 445\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_var\u001b[39m(a: ArrayLike, axis: Axis \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m, dtype: DTypeLike \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 446\u001b[0m out: \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m, correction: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m|\u001b[39m \u001b[38;5;28mfloat\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m0\u001b[39m, keepdims: \u001b[38;5;28mbool\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m, \u001b[38;5;241m*\u001b[39m,\n\u001b[1;32m 447\u001b[0m where: ArrayLike \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m--> 448\u001b[0m \u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mvar\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 449\u001b[0m dtypes\u001b[38;5;241m.\u001b[39mcheck_user_dtype_supported(dtype, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mvar\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 450\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m out \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: var requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.var(input)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.where.ipynb b/api-examples/operator/ops.where.ipynb new file mode 100644 index 0000000..c996c92 --- /dev/null +++ b/api-examples/operator/ops.where.ipynb @@ -0,0 +1,207 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.where(condition, input, other) -〉 Tensor\n", + "根据条件从输入中选择元素组成返回tensor。\n", + "- 输入:\n", + " * condition:Tensor[bool]。\n", + " * input:Tensor, Scalar。\n", + " * other:Tensor, Scalar。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| condition | condition | condition |\n", + "| input | input | x |\n", + "| other | other | y |\n", + "| | out | size |\n", + "| | | fill_value |\n", + "\n", + "* torch额外提供了out出参方式。\n", + "* jax提供了size和fill_value用于处理当x和y为空的情况。ms与torch的input和other均为必选参数。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[0. 0.3139]\n", + " [0.3898 0. ]\n", + " [0.0478 0. ]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[0.0000, 0.3139],\n", + " [0.3898, 0.0000],\n", + " [0.0478, 0.0000]], dtype=torch.float64)\n", + "\n", + "\n", + "jax output:\n", + " [[0. 0.3139]\n", + " [0.3898 0. ]\n", + " [0.0478 0. ]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[-0.4620, 0.3139],\n", + " [ 0.3898, -0.7197],\n", + " [ 0.0478, -0.1657]])\n", + "\n", + "y1 = ms.ops.where(ms.tensor(input) > 0, ms.tensor(input), 0)\n", + "y2 = torch.where(torch.tensor(input) > 0, torch.tensor(input), 0)\n", + "y3 = jnp.where(input > 0, input, 0)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling Select with \"Select()(condition=, input=, other=int)\".\nThe valid calling should be: \n\"Select()(condition=, input=, other=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/wy/5yzfdb6x7pvfxh89zcmf_w780000gn/T/ipykernel_99151/397926283.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0my1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwhere\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/array_func.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(condition, input, other)\u001b[0m\n\u001b[1;32m 515\u001b[0m \u001b[0;34m>>\u001b[0m\u001b[0;34m>\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 516\u001b[0m [[0. 1.]\n\u001b[1;32m 517\u001b[0m [2. 1.]]\n\u001b[1;32m 518\u001b[0m \"\"\"\n\u001b[0;32m--> 519\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mtensor_select_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcondition\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(self, condition, input, other)\u001b[0m\n\u001b[1;32m 13815\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcondition\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m> 13816\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_convert_stub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpyboost_select\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mcondition\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mother\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Select with \"Select()(condition=, input=, other=int)\".\nThe valid calling should be: \n\"Select()(condition=, input=, other=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.where(input > 0, input, 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "where() received an invalid combination of arguments - got (numpy.ndarray, numpy.ndarray, int), but expected one of:\n * (Tensor condition)\n * (Tensor condition, Tensor input, Tensor other, *, Tensor out)\n * (Tensor condition, Number self, Tensor other)\n didn't match because some of the arguments have invalid types: (!numpy.ndarray!, !numpy.ndarray!, !int!)\n * (Tensor condition, Tensor input, Number other)\n didn't match because some of the arguments have invalid types: (!numpy.ndarray!, !numpy.ndarray!, !int!)\n * (Tensor condition, Number self, Number other)\n didn't match because some of the arguments have invalid types: (!numpy.ndarray!, !numpy.ndarray!, !int!)\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[8], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwhere\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m>\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: where() received an invalid combination of arguments - got (numpy.ndarray, numpy.ndarray, int), but expected one of:\n * (Tensor condition)\n * (Tensor condition, Tensor input, Tensor other, *, Tensor out)\n * (Tensor condition, Number self, Tensor other)\n didn't match because some of the arguments have invalid types: (!numpy.ndarray!, !numpy.ndarray!, !int!)\n * (Tensor condition, Tensor input, Number other)\n didn't match because some of the arguments have invalid types: (!numpy.ndarray!, !numpy.ndarray!, !int!)\n * (Tensor condition, Number self, Number other)\n didn't match because some of the arguments have invalid types: (!numpy.ndarray!, !numpy.ndarray!, !int!)\n" + ] + } + ], + "source": [ + "y2 = torch.where(input > 0, input, 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'>' not supported between instances of 'list' and 'int'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[9], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m jnp\u001b[38;5;241m.\u001b[39mwhere(\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m>\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m, \u001b[38;5;28minput\u001b[39m, \u001b[38;5;241m0\u001b[39m)\n", + "\u001b[0;31mTypeError\u001b[0m: '>' not supported between instances of 'list' and 'int'" + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.where(input > 0, input, 0)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.zeros.ipynb b/api-examples/operator/ops.zeros.ipynb new file mode 100644 index 0000000..42b818c --- /dev/null +++ b/api-examples/operator/ops.zeros.ipynb @@ -0,0 +1,245 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.ones(size, dtype=None) -〉 Tensor\n", + "创建一个值全为0的Tensor。\n", + "- 输入:\n", + " * size: tuple[int]]、list[int]、int或mindspore的tensor(数据类型为int32或int64)。\n", + " * dtype: mindspore.dtype。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| size | size | shape |\n", + "| dtype | out | dtype |\n", + "| | dtype | device |\n", + "| | layout | |\n", + "| | device | |\n", + "| | requires_grad | |\n", + "\n", + "\n", + "* torch额外提供了out出参方式, layout用于设置内存存储结构,以及requires_grad设置自动求导是否记录操作。\n", + "* torch和jax都提供了device参数,用于设置返回值提交到哪个设备上。\n", + "* mindspore.ops.ones与mindspore.ops.zeros参数名不一致。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[0. 0. 0.]\n", + " [0. 0. 0.]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[0., 0., 0.],\n", + " [0., 0., 0.]])\n", + "\n", + "\n", + "jax output:\n", + " [[0. 0. 0.]\n", + " [0. 0. 0.]]\n" + ] + } + ], + "source": [ + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "y1 = ms.ops.zeros((2,3))\n", + "y2 = torch.zeros((2,3))\n", + "y3 = jnp.zeros((2,3))\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "ab94efa5-f405-4772-a489-9f0e5e502303", + "metadata": {}, + "source": [ + "指定数据类型:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "21aa93b7-45b9-4712-a930-380bd4b6f502", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [False False False False]\n", + "\n", + "\n", + "torch output:\n", + " tensor([False, False, False, False])\n", + "\n", + "\n", + "jax output:\n", + " [False False False False]\n" + ] + } + ], + "source": [ + "y1 = ms.ops.zeros(4, dtype=ms.bool_)\n", + "y2 = torch.zeros(4, dtype=bool)\n", + "y3 = jnp.zeros(4, dtype=bool)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "91de2ec0-81fb-4065-82f4-3f918c175d42", + "metadata": {}, + "source": [ + "3、报错信息比较:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling Zeros with \"Zeros()(size=float, dtype=mindspore.dtype)\".\nThe valid calling should be: \n\"Zeros()(size=, dtype=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[3], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mzeros\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1.5\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_def.py:8275\u001b[0m, in \u001b[0;36mzeros\u001b[0;34m(size, dtype)\u001b[0m\n\u001b[1;32m 8244\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mzeros\u001b[39m(size, dtype\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[1;32m 8245\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 8246\u001b[0m \u001b[38;5;124;03m Creates a tensor filled with value zeros, whose shape and type are described by the first argument `size` and second argument `dtype` respectively.\u001b[39;00m\n\u001b[1;32m 8247\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 8273\u001b[0m \u001b[38;5;124;03m [0. 0.]]\u001b[39;00m\n\u001b[1;32m 8274\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 8275\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mzeros_op\u001b[49m\u001b[43m(\u001b[49m\u001b[43msize\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/operations/manually_defined/ops_def.py:1874\u001b[0m, in \u001b[0;36mZeros.__call__\u001b[0;34m(self, size, type)\u001b[0m\n\u001b[1;32m 1873\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, size, \u001b[38;5;28mtype\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[0;32m-> 1874\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_stub(\u001b[43mpyboost_zeros\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[43msize\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mtype\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43mtype\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mis\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;28;43;01melse\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m\\\u001b[49m\n\u001b[1;32m 1875\u001b[0m \u001b[43m \u001b[49m\u001b[43mhandler\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdtype_to_type_id\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mZeros\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mtype\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mtype\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m)\n", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Zeros with \"Zeros()(size=float, dtype=mindspore.dtype)\".\nThe valid calling should be: \n\"Zeros()(size=, dtype=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.zeros(1.5)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "zeros(): argument 'size' (position 1) must be tuple of ints, but found element of type float at pos 0", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mzeros\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1.5\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: zeros(): argument 'size' (position 1) must be tuple of ints, but found element of type float at pos 0" + ] + } + ], + "source": [ + "y2 = torch.zeros(1.5)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Shapes must be 1D sequences of concrete values of integer type, got (1.5,).", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mzeros\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1.5\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:3387\u001b[0m, in \u001b[0;36mzeros\u001b[0;34m(shape, dtype, device)\u001b[0m\n\u001b[1;32m 3385\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (m \u001b[38;5;241m:=\u001b[39m _check_forgot_shape_tuple(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mzeros\u001b[39m\u001b[38;5;124m\"\u001b[39m, shape, dtype)): \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(m)\n\u001b[1;32m 3386\u001b[0m dtypes\u001b[38;5;241m.\u001b[39mcheck_user_dtype_supported(dtype, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mzeros\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m-> 3387\u001b[0m shape \u001b[38;5;241m=\u001b[39m \u001b[43mcanonicalize_shape\u001b[49m\u001b[43m(\u001b[49m\u001b[43mshape\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3388\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m lax\u001b[38;5;241m.\u001b[39mfull(shape, \u001b[38;5;241m0\u001b[39m, _jnp_dtype(dtype), sharding\u001b[38;5;241m=\u001b[39m_normalize_to_sharding(device))\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:98\u001b[0m, in \u001b[0;36mcanonicalize_shape\u001b[0;34m(shape, context)\u001b[0m\n\u001b[1;32m 95\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcanonicalize_shape\u001b[39m(shape: Any, context: \u001b[38;5;28mstr\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m core\u001b[38;5;241m.\u001b[39mShape:\n\u001b[1;32m 96\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(shape, (\u001b[38;5;28mtuple\u001b[39m, \u001b[38;5;28mlist\u001b[39m)) \u001b[38;5;129;01mand\u001b[39;00m\n\u001b[1;32m 97\u001b[0m (\u001b[38;5;28mgetattr\u001b[39m(shape, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mndim\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m \u001b[38;5;129;01mor\u001b[39;00m ndim(shape) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m)):\n\u001b[0;32m---> 98\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mcore\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcanonicalize_shape\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[43mshape\u001b[49m\u001b[43m,\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcontext\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 99\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 100\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m core\u001b[38;5;241m.\u001b[39mcanonicalize_shape(shape, context)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/core.py:1689\u001b[0m, in \u001b[0;36mcanonicalize_shape\u001b[0;34m(shape, context)\u001b[0m\n\u001b[1;32m 1687\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m:\n\u001b[1;32m 1688\u001b[0m \u001b[38;5;28;01mpass\u001b[39;00m\n\u001b[0;32m-> 1689\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m _invalid_shape_error(shape, context)\n", + "\u001b[0;31mTypeError\u001b[0m: Shapes must be 1D sequences of concrete values of integer type, got (1.5,)." + ] + } + ], + "source": [ + "y3 = jnp.zeros(1.5)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.zeros_like.ipynb b/api-examples/operator/ops.zeros_like.ipynb new file mode 100644 index 0000000..b156c08 --- /dev/null +++ b/api-examples/operator/ops.zeros_like.ipynb @@ -0,0 +1,231 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.zeros_like(input, *, dtype=None) -〉 Tensor\n", + "返回值为0的Tensor,shape与输入相同。\n", + "- 输入:\n", + " * input: mindspore的tensor\n", + " * dtype: mindspore.dtype\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | a |\n", + "| dtype | dtype | shape |\n", + "| | layout | dtype |\n", + "| | device | device |\n", + "| | requires_grad | |\n", + "| | memory_format | |\n", + "\n", + "\n", + "* torch额外提供了layout用于设置内存存储结构,requires_grad设置自动求导是否记录操作以及memory_format内存存储方式。\n", + "* jax额外提供了shape参数用于覆盖input的shape。\n", + "* torch和jax都提供了device参数,用于设置返回值提交到哪个设备上。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [0. 0. 0. 0.]\n", + "\n", + "\n", + "torch output:\n", + " tensor([0., 0., 0., 0.], dtype=torch.float64)\n", + "\n", + "\n", + "jax output:\n", + " [0. 0. 0. 0.]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([0., -3., 7, -7])\n", + "\n", + "y1 = ms.ops.zeros_like(ms.tensor(input))\n", + "y2 = torch.zeros_like(torch.tensor(input))\n", + "y3 = jnp.zeros_like(input)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "ab94efa5-f405-4772-a489-9f0e5e502303", + "metadata": {}, + "source": [ + "通过shape参数覆盖input的shape:" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "21aa93b7-45b9-4712-a930-380bd4b6f502", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "jax output:\n", + " [[0. 0. 0.]\n", + " [0. 0. 0.]]\n" + ] + } + ], + "source": [ + "y3 = jnp.zeros_like(input, shape=(2,3))\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "91de2ec0-81fb-4065-82f4-3f918c175d42", + "metadata": {}, + "source": [ + "3、报错信息比较:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'float' object has no attribute 'dtype'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mzeros_like\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1.5\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/array_func.py:1095\u001b[0m, in \u001b[0;36mzeros_like\u001b[0;34m(input, dtype)\u001b[0m\n\u001b[1;32m 1063\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mzeros_like\u001b[39m(\u001b[38;5;28minput\u001b[39m, \u001b[38;5;241m*\u001b[39m, dtype\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[1;32m 1064\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1065\u001b[0m \u001b[38;5;124;03m Creates a tensor filled with 0, with the same size as input, and the given dtype.\u001b[39;00m\n\u001b[1;32m 1066\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1093\u001b[0m \u001b[38;5;124;03m [0. 0.]]\u001b[39;00m\n\u001b[1;32m 1094\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1095\u001b[0m _dtype \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43minput\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdtype\u001b[49m \u001b[38;5;28;01mif\u001b[39;00m dtype \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;28;01melse\u001b[39;00m dtype\n\u001b[1;32m 1096\u001b[0m output \u001b[38;5;241m=\u001b[39m zeros_like_(\u001b[38;5;28minput\u001b[39m)\n\u001b[1;32m 1097\u001b[0m output \u001b[38;5;241m=\u001b[39m cast_(output, _dtype)\n", + "\u001b[0;31mAttributeError\u001b[0m: 'float' object has no attribute 'dtype'" + ] + } + ], + "source": [ + "y1 = ms.ops.zeros_like(1.5)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "zeros_like(): argument 'input' (position 1) must be Tensor, not float", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mzeros_like\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1.5\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: zeros_like(): argument 'input' (position 1) must be Tensor, not float" + ] + } + ], + "source": [ + "y2 = torch.zeros_like(1.5)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "zeros_like requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mzeros_like\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:3304\u001b[0m, in \u001b[0;36mzeros_like\u001b[0;34m(a, dtype, shape, device)\u001b[0m\n\u001b[1;32m 3298\u001b[0m \u001b[38;5;129m@util\u001b[39m\u001b[38;5;241m.\u001b[39mimplements(np\u001b[38;5;241m.\u001b[39mzeros_like)\n\u001b[1;32m 3299\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mzeros_like\u001b[39m(a: ArrayLike \u001b[38;5;241m|\u001b[39m DuckTypedArray,\n\u001b[1;32m 3300\u001b[0m dtype: DTypeLike \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 3301\u001b[0m shape: Any \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;241m*\u001b[39m,\n\u001b[1;32m 3302\u001b[0m device: xc\u001b[38;5;241m.\u001b[39mDevice \u001b[38;5;241m|\u001b[39m Sharding \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[1;32m 3303\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m (\u001b[38;5;28mhasattr\u001b[39m(a, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdtype\u001b[39m\u001b[38;5;124m'\u001b[39m) \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mhasattr\u001b[39m(a, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mshape\u001b[39m\u001b[38;5;124m'\u001b[39m)): \u001b[38;5;66;03m# support duck typing\u001b[39;00m\n\u001b[0;32m-> 3304\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mzeros_like\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 3305\u001b[0m dtypes\u001b[38;5;241m.\u001b[39mcheck_user_dtype_supported(dtype, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mzeros_like\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 3306\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m shape \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: zeros_like requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.zeros_like(input)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 2f40cb01624604418a925f4afada22658e9aba3c Mon Sep 17 00:00:00 2001 From: mengxian Date: Sat, 11 Jan 2025 17:00:50 +0800 Subject: [PATCH 2/5] API --- api-examples/operator/README.md | 32 +++++++++---------- api-examples/operator/ops.inner.ipynb | 2 +- api-examples/operator/ops.isclose.ipynb | 2 +- api-examples/operator/ops.log10.ipynb | 2 +- api-examples/operator/ops.log2.ipynb | 2 +- api-examples/operator/ops.nan_to_num!!!.ipynb | 2 +- api-examples/operator/ops.ones_like.ipynb | 2 +- api-examples/operator/ops.sort.ipynb | 2 +- api-examples/operator/ops.zeros_like.ipynb | 2 +- 9 files changed, 24 insertions(+), 24 deletions(-) diff --git a/api-examples/operator/README.md b/api-examples/operator/README.md index 2e665cc..98c6434 100644 --- a/api-examples/operator/README.md +++ b/api-examples/operator/README.md @@ -10,10 +10,10 @@ | [ops.addcmul](./ops.addcmul.ipynb) | 计算tensor1与tensor2的相乘后乘以value,再加上input | element-wise | [ops.addmm](./ops.addmm!!!.ipynb) | 矩阵与矩阵相乘后加上input | 线性代数 | [ops.addmv](./ops.addmv!!!.ipynb) | 矩阵与向量相乘后加上input | element-wise -| [ops.all](./ops.all.ipynb) | 矩阵与向量相乘后加上input | Reduction函数 +| [ops.all](./ops.all.ipynb) | 返回指定维度上的所有元素是否都为True | Reduction函数 | [ops.angle](./ops.angle.ipynb) | 计算复数Tensor的辐角 | element-wise | [ops.any](./ops.any.ipynb) | 返回指定维度是否含有True | Reduction函数 -| [ops.arange](./ops.arange.ipynb) | 创建指定起止和步长的序列 | 创建 +| [ops.arange](./ops.arange.ipynb) | 按指定起止和步长创建序列 | 创建 | [ops.arccos](./ops.arccos.ipynb) | 计算反余弦 | element-wise | [ops.arccosh](./ops.arccosh.ipynb) | 计算反双曲余弦 | element-wise | [ops.arcsin](./ops.arcsinh.ipynb) | 计算反正弦 | element-wise @@ -57,7 +57,7 @@ | [ops.div](./ops.div.ipynb) | 计算第一个输入除以第二输入的商 | element-wise | [ops.divide](./ops.divide.ipynb) | 计算第一个输入除以第二输入的商 | element-wise | [ops.equal](./ops.equal.ipynb) | 比较两个输入Tensor是否相等 | element-wise -| [ops.erf](./ops.erf.ipynb) | 比较两个输入Tensor是否相等 | element-wise +| [ops.erf](./ops.erf.ipynb) | 计算输入的高斯误差 | element-wise | [ops.erfinv](./ops.erfinv.ipynb) | 计算输入的逆误差函数 | element-wise | [ops.exp](./ops.exp!!!.ipynb) | 计算输入的指数 | element-wise | [ops.expand_dims](./ops.expand_dims.ipynb) | 对输入在给定的轴上添加额外维度 | Tensors @@ -70,8 +70,8 @@ | [ops.full](./ops.full!!!.ipynb) | 创建一个指定shape的Tensor,并用指定值填充 | 创建 | [ops.greater_equal](./ops.greater_equal.ipynb) | 比较第一个输入是否大于等于第二个输入 | 比较函数 | [ops.greater](./ops.greater.ipynb) | 比较第一个输入是否大于第二个输入 | 比较函数 -| [ops.inner](./ops.inner.ipynb) | 计算输入的的高斯误差 | 比较函数 -| [ops.isclose](./ops.isclose.ipynb) | 计算输入的的高斯误差 | 比较函数 +| [ops.inner](./ops.inner.ipynb) | 计算两个1D Tensor的点积 | 比较函数 +| [ops.isclose](./ops.isclose.ipynb) | 比较两个Tensor的元素是否接近 | 比较函数 | [ops.isfinite](./ops.isfinite.ipynb) | 判断输入数据的每个元素是否是有限数 | 比较函数 | [ops.isinf](./ops.isinf.ipynb) | 判断输入数据的每个元素是否是无限数 | 比较函数 | [ops.isnan](./ops.isnan.ipynb) | 判断输入数据的每个元素是否是nan值 | 比较函数 @@ -80,11 +80,11 @@ | [ops.less_equal](./ops.less_equal.ipynb) | 比较第一个输入是否小于等于第二个输入 | 比较函数 | [ops.less](./ops.less.ipynb) | 比较第一个输入是否小于第二个输入 | 比较函数 | [ops.linspace](./ops.linspace.ipynb) | 返回一个在区间start和end内的包含steps个值的一维Tensor | 创建 -| [ops.log](./ops.log.ipynb) | 计算输入的逆误差函数 | element-wise -| [ops.log1p](./ops.log1p.ipynb) | 计算输入的逆误差函数 | element-wise -| [ops.log2](./ops.log2.ipynb) | 计算输入的逆误差函数 | element-wise -| [ops.log10](./ops.log10.ipynb) | 计算输入的逆误差函数 | element-wise -| [ops.logaddexp](./ops.logaddexp.ipynb) | 计算输入的逆误差函数 | element-wise +| [ops.log](./ops.log.ipynb) | 返回输入的自然对数 | element-wise +| [ops.log1p](./ops.log1p.ipynb) | 对输入加一后计算自然对数 | element-wise +| [ops.log2](./ops.log2.ipynb) | 返回以2为底的输入的对数 | element-wise +| [ops.log10](./ops.log10.ipynb) | 返回以10为底的输入的数 | element-wise +| [ops.logaddexp](./ops.logaddexp.ipynb) | 对两个输入分别求指数后相加再求对数 | element-wise | [ops.logical_and](./ops.logical_and.ipynb) | 计算两个输入的逻辑与 | element-wise | [ops.logical_not](./ops.logical_not.ipynb) | 计算输入的逻辑非 | element-wise | [ops.logical_or](./ops.logical_or.ipynb) | 计算两个输入的逻辑或 | element-wise @@ -99,12 +99,12 @@ | [ops.moveaxis](./ops.moveaxis.ipynb) | 将x在source中位置的维度移动到destination中的位置 | 索引、切片、连接、变形 | [ops.mul](./ops.mul.ipynb) | 计算两个Tensor逐元素相乘| element-wise | [ops.multiply](./ops.multiply.ipynb) | 计算两个Tensor逐元素相乘。 | element-wise -| [ops.nan_to_num](./ops.nan_to_num!!!.ipynb) | 计算两个Tensor逐元素相乘。 | element-wise +| [ops.nan_to_num](./ops.nan_to_num!!!.ipynb) | 将输入中的nan、posinf和neginf替换为指定的值 | element-wise | [ops.neg](./ops.neg.ipynb) | 计算输入的相反数 | element-wise | [ops.negative](./ops.negative.ipynb) | 计算输入的相反数 | element-wise | [ops.not_equal](./ops.not_equal.ipynb) | 计算两个Tensor的元素是否不相等 | 比较函数 | [ops.ones](./ops.ones.ipynb) | 创建一个值全为1的Tensor | element-wise -| [ops.ones_like](./ops.ones_like.ipynb) | 返回值为1的Tensor,shape与输入相同 | element-wise +| [ops.ones_like](./ops.ones_like.ipynb) | 创建shape与输入相同,但值为1的Tensor | element-wise | [ops.outer](./ops.outer.ipynb) | 计算两个输入的外积 | 线性代数 | [ops.pad](./ops.pad.ipynb) | 对输入进行填充 | 神经网络 | [ops.pow](./ops.pow!!!.ipynb) | 计算input中每个元素的exponent次幂 | element-wise @@ -122,8 +122,8 @@ | [ops.sin](./ops.sin.ipynb) | 计算输入的正弦值 | element-wise | [ops.sinh](./ops.sinh.ipynb) | 计算输入的双曲正弦值 | element-wise | [ops.softmax](./ops.softmax.ipynb) | 指定轴上对输入做Softmax归一化操作 | element-wise -| [ops.sort](./ops.sort.ipynb) | 指定轴上对输入做Softmax归一化操作 | 比较函数 -| [ops.split](./ops.split.ipynb) | 指定轴上对输入做Softmax归一化操作 | 索引、切片、连接、变形 +| [ops.sort](./ops.sort.ipynb) | 按指定顺序、指定维度对输入的元素进行排序 | 比较函数 +| [ops.split](./ops.split.ipynb) | 按指定维度对输入进行切分 | 索引、切片、连接、变形 | [ops.sqrt](./ops.sqrt.ipynb) | 返回输入的平方根 | element-wise | [ops.square](./ops.square.ipynb) | 返回输入的平方 | element-wise | [ops.squeeze](./ops.squeeze.ipynb) | 返回删除指定维度中大小为1的维度后的Tensor | 索引、切片、连接、变形 @@ -137,13 +137,13 @@ | [ops.tanh](./ops.tanh.ipynb) | 计算输入的双曲正切值 | element-wise | [ops.tensor_dot](./ops.tensor_dot.ipynb) | 在指定轴上对两个输入进行点乘操作 | 线性代数 | [ops.tile](./ops.tile.ipynb) | 根据指定维度复制input | 索引、切片、连接、变形 -| [ops.topk](./ops.topk.ipynb) | 沿给定维度查找 k 个最大或最小元素和对应的索引 | 比较函数 +| [ops.topk](./ops.topk.ipynb) | 沿给定维度查找k个最大或最小元素和对应的索引 | 比较函数 | [ops.trace](./ops.trace.ipynb) | 返回二维输入在主对角线方向上的元素总和 | 线性代数 | [ops.transpose](./ops.transpose.ipynb) | 按指定的排列对输入进行数据重排 | 索引、切片、连接、变形 | [ops.tril](./ops.tril.ipynb) | 返回输入的对角线和下面的元素,并将其他元素设置为0 | Tensors | [ops.triu](./ops.triu.ipynb) | 返回输入的对角线和上面的元素,并将其他元素设置为0 | Tensors | [ops.var](./ops.var.ipynb) | 按指定维度返回输入Tensor上的方差 | Reduction函数 | [ops.where](./ops.where.ipynb) | 根据条件从输入中选择元素组成返回 | Tensor -| [ops.zeros_like](./ops.zeros_like.ipynb) | 返回值为0的Tensor,shape与输入相同 | 创建 +| [ops.zeros_like](./ops.zeros_like.ipynb) | 创建shape与输入相同,但值为0的Tensor | 创建 | [ops.zeros](./ops.zeros.ipynb) | 创建一个值全为0的Tensor | 创建 diff --git a/api-examples/operator/ops.inner.ipynb b/api-examples/operator/ops.inner.ipynb index a069bf5..b776afc 100644 --- a/api-examples/operator/ops.inner.ipynb +++ b/api-examples/operator/ops.inner.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "source": [ "## mindspore.ops.inner(input, other) -〉 Tensor\n", - "对于1D Tensor,返回两个向量的点积。对于更高的维度,返回最后一个轴上的和积。\n", + "计算两个1D Tensor的点积。\n", "\n", "- 输入:input和other必须mindspore的tensor。\n", "- 返回:mindspore的tensor。" diff --git a/api-examples/operator/ops.isclose.ipynb b/api-examples/operator/ops.isclose.ipynb index ab32592..ddd0bbf 100644 --- a/api-examples/operator/ops.isclose.ipynb +++ b/api-examples/operator/ops.isclose.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "source": [ "## mindspore.ops.isclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False) -〉 Tensor\n", - "返回input的每个元素与other的对应元素在给定容忍度内是否“接近”。\n", + "比较两个Tensor的元素是否接近。\n", "\n", "- 输入:\n", " * input:mindspore的tensor, 数据类型有float16、float32、float64、int8、int16、int32、int64、uint8,Ascend平台额外支持bfloat16和bool类型\n", diff --git a/api-examples/operator/ops.log10.ipynb b/api-examples/operator/ops.log10.ipynb index e10fa6a..adead55 100644 --- a/api-examples/operator/ops.log10.ipynb +++ b/api-examples/operator/ops.log10.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "source": [ "## mindspore.ops.log10(input) -〉 Tensor\n", - "返回输入以10为底的对数。\n", + "返回以10为底的输入的对数。\n", "\n", "- 输入:input必须为mindspore的tensor,值>0。\n", "- 返回:mindspore的tensor,shape与input的shape相同。" diff --git a/api-examples/operator/ops.log2.ipynb b/api-examples/operator/ops.log2.ipynb index 8061c57..6c6eada 100644 --- a/api-examples/operator/ops.log2.ipynb +++ b/api-examples/operator/ops.log2.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "source": [ "## mindspore.ops.log2(input) -〉 Tensor\n", - "返回输入以2为底的对数。\n", + "返回以2为底的输入的对数。\n", "- 输入:input必须为mindspore的tensor,值>0。\n", "- 返回:mindspore的tensor,shape与input的shape相同。" ] diff --git a/api-examples/operator/ops.nan_to_num!!!.ipynb b/api-examples/operator/ops.nan_to_num!!!.ipynb index ae45a95..ebd64b4 100644 --- a/api-examples/operator/ops.nan_to_num!!!.ipynb +++ b/api-examples/operator/ops.nan_to_num!!!.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "source": [ "## mindspore.ops.nan_to_num(input, nan=None, posinf=None, neginf=None) -〉 Tensor\n", - "返回值为1的Tensor,shape与输入相同。\n", + "将输入中的nan、posinf和neginf替换为指定的值。\n", "- 输入:\n", " * input: mindspore的tensor。\n", " * nan: number。\n", diff --git a/api-examples/operator/ops.ones_like.ipynb b/api-examples/operator/ops.ones_like.ipynb index c15f848..262ff74 100644 --- a/api-examples/operator/ops.ones_like.ipynb +++ b/api-examples/operator/ops.ones_like.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "source": [ "## mindspore.ops.ones_like(input, *, dtype=None) -〉 Tensor\n", - "返回值为1的Tensor,shape与输入相同。\n", + "创建shape与输入相同,但值为1的Tensor。\n", "- 输入:\n", " * input: mindspore的tensor\n", " * dtype: mindspore.dtype\n", diff --git a/api-examples/operator/ops.sort.ipynb b/api-examples/operator/ops.sort.ipynb index a4bc438..9f55030 100644 --- a/api-examples/operator/ops.sort.ipynb +++ b/api-examples/operator/ops.sort.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "source": [ "## mindspore.ops.sort(input_x, axis=- 1, descending=False) -〉 Tensor Tensor\n", - "计算输入的相反数。\n", + "按指定顺序、指定维度对输入的元素进行排序。\n", "- 输入:\n", " * input_x:mindspore的tensor。\n", " * axis:int。\n", diff --git a/api-examples/operator/ops.zeros_like.ipynb b/api-examples/operator/ops.zeros_like.ipynb index b156c08..ceabc50 100644 --- a/api-examples/operator/ops.zeros_like.ipynb +++ b/api-examples/operator/ops.zeros_like.ipynb @@ -6,7 +6,7 @@ "metadata": {}, "source": [ "## mindspore.ops.zeros_like(input, *, dtype=None) -〉 Tensor\n", - "返回值为0的Tensor,shape与输入相同。\n", + "创建shape与输入相同,但值为0的Tensor。\n", "- 输入:\n", " * input: mindspore的tensor\n", " * dtype: mindspore.dtype\n", From ff0c546eb2f56db2dd4e4f9c0f918b86f7504b24 Mon Sep 17 00:00:00 2001 From: mengxian Date: Wed, 15 Jan 2025 18:41:48 +0800 Subject: [PATCH 3/5] API --- .DS_Store | Bin 6148 -> 6148 bytes api-examples/.DS_Store | Bin 8196 -> 10244 bytes api-examples/operator/README.md | 6 +- .../tensor.max-checkpoint.ipynb | 204 ++++++++++ api-examples/tensor/tensor.max.ipynb | 373 ++++++++++++++++++ 5 files changed, 580 insertions(+), 3 deletions(-) create mode 100644 api-examples/tensor/.ipynb_checkpoints/tensor.max-checkpoint.ipynb create mode 100644 api-examples/tensor/tensor.max.ipynb diff --git a/.DS_Store b/.DS_Store index 44989d9390b00bd756445c50898a18b6f9889db0..0c050675ebba649f3c051d9347ed8e074d3cf115 100644 GIT binary patch delta 65 zcmZoMXffC@nT2udOvv3Fi0Ev|n*8l(j delta 73 zcmZoMXffC@nT2u3Jrt}CdL*z3WnwewK@vbmPQ6T3MR&8wY8iaqRRT#LGjr+ dxq10rlQmi88M`Ovu*hsqWu4EonVsV=KL8}i6{P?G diff --git a/api-examples/.DS_Store b/api-examples/.DS_Store index 66bdd6e8e8ce032c8f2a46483306741967546fe3..472f956ddf8a357cc282b4b4cf7c67f9340b2d9f 100644 GIT binary patch literal 10244 zcmeHMTWl0n7(U;$lo>kEX{B6-7FMc&*z`gbxrs|JmQqNO?Y(H--5KcE=}g_3ZMmee zL?hmwyuD&j6B89R0pCQU@$#UFno1%>^pOYSlP`c#|1)Q{VcRM%8qmy1X8t+<`TzgS z|LynB$vMjyLu1ycXDr4Tlj-78Q|UHG7ZM%1t3{S>Y&jx z?E?azSSaJ6oD?!}rI=#(fY24;76Zba#$()^WIU9ULJD^V;m#1wjBtm7V0QA01?CKK zA)|YQK!m_-1o+;)jMXrUjk3de`}Z^(CY?(o4)dAUX1nQ>?WV|{N(_}GYyJpY5L711 zQQGxqSdy$=mdUqgJFaJ+8uv!S_-9h1w&nSCb>|=z6wWJ}FVB~Y?lx#wi6+cL)owZ%=Y<0mc0%mfaWVUo%I9?LP@@kTf67+&C;5CgIzD}!43 z(W7j1S zlXg05Cw+5=ZF6JNm3ZllYukHW&+;wT>Ff2)Lw+t$-cWD9=Vtt-AlRU$Wj%ABNwKUk zvza1Ns4_lV$0!fub(db?#KwevR8`gP<gv8H^DAgE?4EqRTW<^9s?AW#W{T_`Px~@*04>qR(7%jRDH+TbL<2= z%|2jf*;niwyT~rFU)W`KgRFs+&bk+Kb28BvbD-hl4ihC4U zgXnBgw<%PFTH(|RefhHOYFyPA{|G3#NG-Cg$^Q_)zhURuPsHzE*<}==7#db#9qOAHWbS48up3n0*LG@Hn2plQ@p2iQO*{zd5^K!AZP|*KrE( z;ys+k8Jxw3_y`~46MTv9a2^+CaCmno6(WN-E=i)a7N_W!WL<|RJ^O3FQI5dskc5dskc5dskc*F&H{ z7{vJbf6HJ0|9`zO(RhSFgus6o0TebTnj0yB+-7`cF8r)LM%Tl1@y6{Yg$!Ki#!a|B yIF6?dKaO7)>cTUmU_2h`CWQ>#$uDpxDNghM^*;k{_;Fga|Bv?nK^fh!{r{h8vK0{k literal 8196 zcmeHMTWl3Y7@mJy%C24LwzN=6S57z(0ZTcwBITy1r>z17iajkoy~ttD*_E#B*)6+! zPQg;sm>7+UmnV(4h%ervBq~M|jZY%pUQ8vyL?28<6JK~xUnJr`GkZ#*p zng22~|9<~W|5HK;B=Y(?LTU*iB34S3a%^TPluzr7A_OyKNCEW;(TGnR;%>K`>TClb(jKqok5s%VpE4-lwAkYWH=M}CBgBaH`if>W+efU6S-R)!!$0k%5o zMVL5&IHzG7dLZ<`Ob^i9UBsMygc!t~p1)@-Cxbj1POVrQFS3HYS>!XL-bf&SGBaYC zo*#>y0imRHPT5>>u2?SKn;dpVlfLHXy@cit(ov7&Seas9%j`9c(X>+C=GeYw+J?&% zOx-|{p+3{rozaAow{?%{COJctL@BM*jE%(_BF!7e8zN&Ho3Pu`eEaygB-TXNZthMW zv~srd2-`M+;O-#v_;{*)bhblTUZIU2sw7oB)c3_hNjH_t^VC%Tz~GRaR;miP`>qk+ zbZl?0;d)5ZN`;4~+@5vpJ+49deObrK=WH*nRAx0Ro3}LIXtk`Q`GA3Q6<*eLENhSB znbb^w$~O-ClsAKw8t@#~Z)3(%CfesxO5QWN4K!*NjCM5bRZks{cR+e){-UMJSFUN= zu%)$Q$L{XQ3Z=42mTMOF6->{}SVq6*Wer<5?E}4|rtS|9pqp*qyw5aTNvZ_5$JTPj zWL53rx+V3h%4II;6s@H48S_xy^p7Y}QJkz&Dk~!DGI>bHJ?(OIYE2iFS0%<-p)OMg zDW}afu+;;FRqCyBkB5gB6^#ov$}zd;AmTt$JU-JbuUC65En`@cqVsiH zv#*LAs??#IzT?szqeHruscJtp*Q)ExQeExhDz!)5D=OcLX+<&Zf{F0QFUzAqjK-fH zV&5eiU(LTywD`T4o1^g|*9@;>>DoFd_ z0+c`*ltVSt!E$JT^{@e2U?U`;3-&-aBw+}K!G!z4gTrtH#^5MC43EL%a15S=XW=Dy z8D4?o@Fu(q@4-p<06vA&@CAGg-@qC88P35ka321G3-C8w6hxs;SR&L5D}=Q|Ojsvu z5;hB4g*$|Mgl=I_(1o1f3S;~TLNV_46^|IqA4NLj^1-+ZE)(Iz>~U9r_qOdjI{!Oy zcXmFFv<1~Q3l~LJtX_Lt%htpcA~}SMsGS}QEC4PG8x{Zz9akWCl7*yv^U`7rUW!T{ zrM(zA#$l1dUzr<7N0*BjIm(=1xN;Q^JC!m%tY6b8Vw5TZ8_M-f%@W3DrcKA3z=r1BrB#4t!DD z$u6XgBu8}mYx%U-_5?~ze{FGijY, dims=Tuple)\".\nThe valid calling should be: \n\"Tile()(input=, dims=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtile\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/operations/manually_defined/ops_def.py:1095\u001b[0m, in \u001b[0;36mtile\u001b[0;34m(input, dims)\u001b[0m\n\u001b[1;32m 1028\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1029\u001b[0m \u001b[38;5;124;03mCreates a new tensor by replicating `input` `dims` times. The i'th dimension of\u001b[39;00m\n\u001b[1;32m 1030\u001b[0m \u001b[38;5;124;03moutput tensor has `input.shape[i] * dims[i]` elements, and the values of `input`\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1092\u001b[0m \u001b[38;5;124;03m [3. 4. 3. 4.]]]\u001b[39;00m\n\u001b[1;32m 1093\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1094\u001b[0m tile_op \u001b[38;5;241m=\u001b[39m _get_cache_prim(Tile)()\n\u001b[0;32m-> 1095\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mtile_op\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdims\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/operations/manually_defined/ops_def.py:1008\u001b[0m, in \u001b[0;36mTile.__call__\u001b[0;34m(self, input, dims)\u001b[0m\n\u001b[1;32m 1007\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m__call__\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;28minput\u001b[39m, dims):\n\u001b[0;32m-> 1008\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _convert_stub(\u001b[43mpyboost_tile\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdims\u001b[49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m)\n", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Tile with \"Tile()(input=, dims=Tuple)\".\nThe valid calling should be: \n\"Tile()(input=, dims=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.tile(input, (2, 3))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "tile(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtile\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: tile(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.tile(input, (2, 3))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "tile requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtile\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:2859\u001b[0m, in \u001b[0;36mtile\u001b[0;34m(A, reps)\u001b[0m\n\u001b[1;32m 2857\u001b[0m \u001b[38;5;129m@util\u001b[39m\u001b[38;5;241m.\u001b[39mimplements(np\u001b[38;5;241m.\u001b[39mtile)\n\u001b[1;32m 2858\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtile\u001b[39m(A: ArrayLike, reps: DimSize \u001b[38;5;241m|\u001b[39m Sequence[DimSize]) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m-> 2859\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtile\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mA\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2860\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 2861\u001b[0m \u001b[38;5;28miter\u001b[39m(reps) \u001b[38;5;66;03m# type: ignore[arg-type]\u001b[39;00m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: tile requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.tile(input, (2, 3))" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/tensor/tensor.max.ipynb b/api-examples/tensor/tensor.max.ipynb new file mode 100644 index 0000000..a018519 --- /dev/null +++ b/api-examples/tensor/tensor.max.ipynb @@ -0,0 +1,373 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.Tensor.max() -> Tensor\n", + "返回输入Tensor的最大值。\n", + "## mindspore.Tensor.max(dim, keepdim=False) -> tuple(Tensor)\n", + "按指定维度返回输入Tensor的最大值及其索引。\n", + "## mindspore.Tensor.max(axis=None, keepdims=False, *, initial=None, where=True, return_indices=False) -> Tensor or scalar\n", + "返回输入Tensor的最大值或最大值及索引。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较: \n", + "| mindspore | | |\n", + "| :----: | :----: | :----: |\n", + "| 重载1 | 重载2 | 重载3 |\n", + "| | dim | axis |\n", + "| | keepdim | keepdims |\n", + "| | | initial |\n", + "| | | where |\n", + "| | | return_indices|\n", + "\n", + "\n", + "| torch | | |\n", + "| :----: | :----: | :----: |\n", + "| 重载1 | 重载2 | 重载3 |\n", + "| | dim | other |\n", + "| | keepdim | out |\n", + "| | out | |\n", + "\n", + "| jax |\n", + "| :----: |\n", + "| axis |\n", + "| out |\n", + "| keepdims |\n", + "| initial |\n", + "| where |\n" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "markdown", + "id": "4a60709b-e71a-404b-ad89-bcd906d38d1c", + "metadata": {}, + "source": [ + "重载1:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " 9\n", + "\n", + "\n", + "torch output:\n", + " tensor(9)\n", + "\n", + "\n", + "jax output:\n", + " 9\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax\n", + "\n", + "input = np.array([[9, 3, 4, 5],\n", + " [5, 2, 7, 4],\n", + " [8, 1, 3, 6]])\n", + "\n", + "y1 = ms.tensor(input)\n", + "y1_output = y1.max()\n", + "y2 = torch.tensor(input)\n", + "y2_output = y2.max()\n", + "y3_output = input.max()\n", + "print ('mindspore output:\\n',y1_output)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2_output)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3_output)" + ] + }, + { + "cell_type": "markdown", + "id": "6d461b44-93c6-43a8-a6ca-160b0b82c743", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "43e8eb29-9488-4172-8645-7ee376c94220", + "metadata": {}, + "source": [ + "重载2:" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9f3d8f1b-4539-4b5b-83c2-4221a401a60d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " (Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[9],\n", + " [7],\n", + " [8]]), Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[0],\n", + " [2],\n", + " [0]]))\n", + "\n", + "\n", + "torch output:\n", + " torch.return_types.max(\n", + "values=tensor([[9],\n", + " [7],\n", + " [8]]),\n", + "indices=tensor([[0],\n", + " [2],\n", + " [0]]))\n", + "\n", + "\n", + "jax output:\n", + " [[9]\n", + " [7]\n", + " [8]]\n" + ] + } + ], + "source": [ + "y1 = ms.tensor(input)\n", + "y1_output = y1.max(dim=1, keepdim=True)\n", + "y2 = torch.tensor(input)\n", + "y2_output = y2.max(dim=1, keepdim=True)\n", + "y3 = input\n", + "y3_output = y3.max(axis=1, keepdims=True)\n", + "print ('mindspore output:\\n',y1_output)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2_output)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3_output)" + ] + }, + { + "cell_type": "markdown", + "id": "e4fb7204-1661-4540-9441-f6ab68d967f7", + "metadata": {}, + "source": [ + "* jax不返回最大值的索引。" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "重载3:ms与jax" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "209694eb-6a58-43c1-a450-9450d2ebf09d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " (Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[0],\n", + " [4],\n", + " [8]]), Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[0],\n", + " [3],\n", + " [0]]))\n", + "\n", + "\n", + "jax output:\n", + " [[0]\n", + " [4]\n", + " [8]]\n" + ] + } + ], + "source": [ + "where=np.array([[0, 0, 0, 0],\n", + " [0, 1, 0, 1],\n", + " [1, 1, 1, 0]], dtype=bool)\n", + "\n", + "y1 = ms.tensor(input)\n", + "y1_output = y1.max(axis=1, keepdims=True, initial=0, where=ms.tensor(where), return_indices=True)\n", + "y3 = input\n", + "y3_output = y3.max(axis=1, keepdims=True, initial=0, where=ms.tensor(where))\n", + "print ('mindspore output:\\n',y1_output)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3_output)" + ] + }, + { + "cell_type": "markdown", + "id": "8a9a6196-d580-4f57-b5b7-efebefbbdc5c", + "metadata": {}, + "source": [ + "* jax不返回最大值的索引。" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "1f0ea595-beca-48ea-8154-adfb94bca0a8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch output:\n", + " tensor([[10, 3, 4, 5],\n", + " [ 5, 2, 7, 4],\n", + " [ 8, 1, 5, 6]])\n" + ] + } + ], + "source": [ + "other=np.array([[10, 0, 0, 0],\n", + " [0, 1, 0, 1],\n", + " [1, 1, 5, 0]])\n", + "y2 = torch.tensor(input)\n", + "y2_output = y2.max(torch.tensor(other))\n", + "print ('torch output:\\n',y2_output)" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling max with \"max(dim=Tuple, keepdim=bool)\".\nThe valid calling should be:\n\"Tensor.max()\"\n\"Tensor.max(dim=, keepdim=)\"\n\"Tensor.max(axis=, keepdims=, *, initial=, where=, return_indices=)\"\n\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/op_function/converter.h:112 Parse\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[16], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1_output \u001b[38;5;241m=\u001b[39m \u001b[43my1\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: Failed calling max with \"max(dim=Tuple, keepdim=bool)\".\nThe valid calling should be:\n\"Tensor.max()\"\n\"Tensor.max(dim=, keepdim=)\"\n\"Tensor.max(axis=, keepdims=, *, initial=, where=, return_indices=)\"\n\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/op_function/converter.h:112 Parse\n" + ] + } + ], + "source": [ + "y1_output = y1.max(dim=(1,2), keepdim=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "max() received an invalid combination of arguments - got (keepdim=bool, dim=tuple, ), but expected one of:\n * ()\n * (Tensor other)\n * (int dim, bool keepdim)\n didn't match because some of the arguments have invalid types: (!dim=tuple of (int, int)!, keepdim=bool, )\n * (name dim, bool keepdim)\n didn't match because some of the arguments have invalid types: (!dim=tuple of (int, int)!, keepdim=bool, )\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[17], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2_output \u001b[38;5;241m=\u001b[39m \u001b[43my2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: max() received an invalid combination of arguments - got (keepdim=bool, dim=tuple, ), but expected one of:\n * ()\n * (Tensor other)\n * (int dim, bool keepdim)\n didn't match because some of the arguments have invalid types: (!dim=tuple of (int, int)!, keepdim=bool, )\n * (name dim, bool keepdim)\n didn't match because some of the arguments have invalid types: (!dim=tuple of (int, int)!, keepdim=bool, )\n" + ] + } + ], + "source": [ + "y2_output = y2.max(dim=(1,2), keepdim=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "tile requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtile\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:2859\u001b[0m, in \u001b[0;36mtile\u001b[0;34m(A, reps)\u001b[0m\n\u001b[1;32m 2857\u001b[0m \u001b[38;5;129m@util\u001b[39m\u001b[38;5;241m.\u001b[39mimplements(np\u001b[38;5;241m.\u001b[39mtile)\n\u001b[1;32m 2858\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtile\u001b[39m(A: ArrayLike, reps: DimSize \u001b[38;5;241m|\u001b[39m Sequence[DimSize]) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m-> 2859\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtile\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mA\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2860\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 2861\u001b[0m \u001b[38;5;28miter\u001b[39m(reps) \u001b[38;5;66;03m# type: ignore[arg-type]\u001b[39;00m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: tile requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "y3_output = y1.max(dim=(1,2), keepdim=True)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 2bf4501e67f8ae00291564d4a3596f4e60fe7f49 Mon Sep 17 00:00:00 2001 From: mengxian Date: Thu, 16 Jan 2025 09:46:39 +0800 Subject: [PATCH 4/5] API --- .../tensor.min-checkpoint.ipynb | 372 ++++++++++++++++++ api-examples/tensor/tensor.max.ipynb | 19 +- api-examples/tensor/tensor.min.ipynb | 372 ++++++++++++++++++ 3 files changed, 753 insertions(+), 10 deletions(-) create mode 100644 api-examples/tensor/.ipynb_checkpoints/tensor.min-checkpoint.ipynb create mode 100644 api-examples/tensor/tensor.min.ipynb diff --git a/api-examples/tensor/.ipynb_checkpoints/tensor.min-checkpoint.ipynb b/api-examples/tensor/.ipynb_checkpoints/tensor.min-checkpoint.ipynb new file mode 100644 index 0000000..5744ccb --- /dev/null +++ b/api-examples/tensor/.ipynb_checkpoints/tensor.min-checkpoint.ipynb @@ -0,0 +1,372 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.Tensor.min() -> Tensor\n", + "返回输入Tensor的最大值。\n", + "## mindspore.Tensor.min(dim, keepdim=False) -> tuple(Tensor)\n", + "按指定维度返回输入Tensor的最大值及其索引。\n", + "## mindspore.Tensor.min(axis=None, keepdims=False, *, initial=None, where=True, return_indices=False) -> Tensor or scalar\n", + "返回输入Tensor的最大值或最大值及索引。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较: \n", + "| mindspore | | |\n", + "| :----: | :----: | :----: |\n", + "| 重载1 | 重载2 | 重载3 |\n", + "| | dim | axis |\n", + "| | keepdim | keepdims |\n", + "| | | initial |\n", + "| | | where |\n", + "| | | return_indices|\n", + "\n", + "\n", + "| torch | | |\n", + "| :----: | :----: | :----: |\n", + "| 重载1 | 重载2 | 重载3 |\n", + "| | dim | other |\n", + "| | keepdim | out |\n", + "| | out | |\n", + "\n", + "| jax |\n", + "| :----: |\n", + "| axis |\n", + "| out |\n", + "| keepdims |\n", + "| initial |\n", + "| where |\n" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "markdown", + "id": "4a60709b-e71a-404b-ad89-bcd906d38d1c", + "metadata": {}, + "source": [ + "重载1:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " 1\n", + "\n", + "\n", + "torch output:\n", + " tensor(1)\n", + "\n", + "\n", + "jax output:\n", + " 1\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax\n", + "\n", + "input = np.array([[9, 3, 4, 5],\n", + " [5, 2, 7, 4],\n", + " [8, 1, 3, 6]])\n", + "\n", + "y1 = ms.tensor(input)\n", + "y1_output = y1.min()\n", + "y2 = torch.tensor(input)\n", + "y2_output = y2.min()\n", + "y3_output = input.min()\n", + "print ('mindspore output:\\n',y1_output)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2_output)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3_output)" + ] + }, + { + "cell_type": "markdown", + "id": "6d461b44-93c6-43a8-a6ca-160b0b82c743", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "43e8eb29-9488-4172-8645-7ee376c94220", + "metadata": {}, + "source": [ + "重载2:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "9f3d8f1b-4539-4b5b-83c2-4221a401a60d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " (Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[3],\n", + " [2],\n", + " [1]]), Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[1],\n", + " [1],\n", + " [1]]))\n", + "\n", + "\n", + "torch output:\n", + " torch.return_types.min(\n", + "values=tensor([[3],\n", + " [2],\n", + " [1]]),\n", + "indices=tensor([[1],\n", + " [1],\n", + " [1]]))\n", + "\n", + "\n", + "jax output:\n", + " [[3]\n", + " [2]\n", + " [1]]\n" + ] + } + ], + "source": [ + "y1 = ms.tensor(input)\n", + "y1_output = y1.min(dim=1, keepdim=True)\n", + "y2 = torch.tensor(input)\n", + "y2_output = y2.min(dim=1, keepdim=True)\n", + "y3 = input\n", + "y3_output = y3.min(axis=1, keepdims=True)\n", + "print ('mindspore output:\\n',y1_output)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2_output)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3_output)" + ] + }, + { + "cell_type": "markdown", + "id": "e4fb7204-1661-4540-9441-f6ab68d967f7", + "metadata": {}, + "source": [ + "* jax不返回最大值的索引。" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "重载3:ms与jax" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "209694eb-6a58-43c1-a450-9450d2ebf09d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " (Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[0],\n", + " [0],\n", + " [0]]), Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[0],\n", + " [0],\n", + " [0]]))\n", + "\n", + "\n", + "jax output:\n", + " [[0]\n", + " [0]\n", + " [0]]\n" + ] + } + ], + "source": [ + "where=np.array([[0, 0, 0, 0],\n", + " [0, 1, 0, 1],\n", + " [1, 1, 1, 0]], dtype=bool)\n", + "\n", + "y1 = ms.tensor(input)\n", + "y1_output = y1.min(axis=1, keepdims=True, initial=0, where=ms.tensor(where), return_indices=True)\n", + "y3 = input\n", + "y3_output = y3.min(axis=1, keepdims=True, initial=0, where=ms.tensor(where))\n", + "print ('mindspore output:\\n',y1_output)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3_output)" + ] + }, + { + "cell_type": "markdown", + "id": "8a9a6196-d580-4f57-b5b7-efebefbbdc5c", + "metadata": {}, + "source": [ + "* jax不返回最大值的索引。" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "1f0ea595-beca-48ea-8154-adfb94bca0a8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch output:\n", + " tensor([[9, 0, 0, 0],\n", + " [0, 1, 0, 1],\n", + " [1, 1, 3, 0]])\n" + ] + } + ], + "source": [ + "other=np.array([[10, 0, 0, 0],\n", + " [0, 1, 0, 1],\n", + " [1, 1, 5, 0]])\n", + "y2 = torch.tensor(input)\n", + "y2_output = y2.min(torch.tensor(other))\n", + "print ('torch output:\\n',y2_output)" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling min with \"min(dim=Tuple, keepdim=bool)\".\nThe valid calling should be:\n\"Tensor.min()\"\n\"Tensor.min(dim=, keepdim=)\"\n\"Tensor.min(axis=, keepdims=, *, initial=, where=, return_indices=)\"\n\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/op_function/converter.h:112 Parse\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1_output \u001b[38;5;241m=\u001b[39m \u001b[43my1\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmin\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: Failed calling min with \"min(dim=Tuple, keepdim=bool)\".\nThe valid calling should be:\n\"Tensor.min()\"\n\"Tensor.min(dim=, keepdim=)\"\n\"Tensor.min(axis=, keepdims=, *, initial=, where=, return_indices=)\"\n\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/op_function/converter.h:112 Parse\n" + ] + } + ], + "source": [ + "y1_output = y1.min(dim=(1,2), keepdim=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "min() received an invalid combination of arguments - got (keepdim=bool, dim=tuple, ), but expected one of:\n * ()\n * (Tensor other)\n * (int dim, bool keepdim)\n didn't match because some of the arguments have invalid types: (!dim=tuple of (int, int)!, keepdim=bool, )\n * (name dim, bool keepdim)\n didn't match because some of the arguments have invalid types: (!dim=tuple of (int, int)!, keepdim=bool, )\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2_output \u001b[38;5;241m=\u001b[39m \u001b[43my2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmin\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: min() received an invalid combination of arguments - got (keepdim=bool, dim=tuple, ), but expected one of:\n * ()\n * (Tensor other)\n * (int dim, bool keepdim)\n didn't match because some of the arguments have invalid types: (!dim=tuple of (int, int)!, keepdim=bool, )\n * (name dim, bool keepdim)\n didn't match because some of the arguments have invalid types: (!dim=tuple of (int, int)!, keepdim=bool, )\n" + ] + } + ], + "source": [ + "y2_output = y2.min(dim=(1,2), keepdim=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "duplicate value in 'axis'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y3_output \u001b[38;5;241m=\u001b[39m \u001b[43my3\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmin\u001b[49m\u001b[43m(\u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdims\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/numpy/core/_methods.py:45\u001b[0m, in \u001b[0;36m_amin\u001b[0;34m(a, axis, out, keepdims, initial, where)\u001b[0m\n\u001b[1;32m 43\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_amin\u001b[39m(a, axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, out\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, keepdims\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[1;32m 44\u001b[0m initial\u001b[38;5;241m=\u001b[39m_NoValue, where\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m):\n\u001b[0;32m---> 45\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mumr_minimum\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mout\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdims\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minitial\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mwhere\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mValueError\u001b[0m: duplicate value in 'axis'" + ] + } + ], + "source": [ + "y3_output = y3.min(axis=(1,1), keepdims=True)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,ms报错信息与torch对齐。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/tensor/tensor.max.ipynb b/api-examples/tensor/tensor.max.ipynb index a018519..5555816 100644 --- a/api-examples/tensor/tensor.max.ipynb +++ b/api-examples/tensor/tensor.max.ipynb @@ -318,26 +318,25 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 20, "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", "metadata": {}, "outputs": [ { - "ename": "TypeError", - "evalue": "tile requires ndarray or scalar arguments, got at position 0.", + "ename": "ValueError", + "evalue": "duplicate value in 'axis'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[6], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtile\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m3\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:2859\u001b[0m, in \u001b[0;36mtile\u001b[0;34m(A, reps)\u001b[0m\n\u001b[1;32m 2857\u001b[0m \u001b[38;5;129m@util\u001b[39m\u001b[38;5;241m.\u001b[39mimplements(np\u001b[38;5;241m.\u001b[39mtile)\n\u001b[1;32m 2858\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtile\u001b[39m(A: ArrayLike, reps: DimSize \u001b[38;5;241m|\u001b[39m Sequence[DimSize]) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m-> 2859\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtile\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mA\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2860\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 2861\u001b[0m \u001b[38;5;28miter\u001b[39m(reps) \u001b[38;5;66;03m# type: ignore[arg-type]\u001b[39;00m\n", - "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", - "\u001b[0;31mTypeError\u001b[0m: tile requires ndarray or scalar arguments, got at position 0." + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[20], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y3_output \u001b[38;5;241m=\u001b[39m \u001b[43my3\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax\u001b[49m\u001b[43m(\u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdims\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/numpy/core/_methods.py:41\u001b[0m, in \u001b[0;36m_amax\u001b[0;34m(a, axis, out, keepdims, initial, where)\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_amax\u001b[39m(a, axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, out\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, keepdims\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[1;32m 40\u001b[0m initial\u001b[38;5;241m=\u001b[39m_NoValue, where\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m):\n\u001b[0;32m---> 41\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mumr_maximum\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mout\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdims\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minitial\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mwhere\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mValueError\u001b[0m: duplicate value in 'axis'" ] } ], "source": [ - "y3_output = y1.max(dim=(1,2), keepdim=True)" + "y3_output = y3.max(axis=(1,1), keepdims=True)" ] }, { @@ -345,7 +344,7 @@ "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", "metadata": {}, "source": [ - "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + "当输入类型不正确时,ms报错信息与torch对齐。" ] } ], diff --git a/api-examples/tensor/tensor.min.ipynb b/api-examples/tensor/tensor.min.ipynb new file mode 100644 index 0000000..5744ccb --- /dev/null +++ b/api-examples/tensor/tensor.min.ipynb @@ -0,0 +1,372 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.Tensor.min() -> Tensor\n", + "返回输入Tensor的最大值。\n", + "## mindspore.Tensor.min(dim, keepdim=False) -> tuple(Tensor)\n", + "按指定维度返回输入Tensor的最大值及其索引。\n", + "## mindspore.Tensor.min(axis=None, keepdims=False, *, initial=None, where=True, return_indices=False) -> Tensor or scalar\n", + "返回输入Tensor的最大值或最大值及索引。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较: \n", + "| mindspore | | |\n", + "| :----: | :----: | :----: |\n", + "| 重载1 | 重载2 | 重载3 |\n", + "| | dim | axis |\n", + "| | keepdim | keepdims |\n", + "| | | initial |\n", + "| | | where |\n", + "| | | return_indices|\n", + "\n", + "\n", + "| torch | | |\n", + "| :----: | :----: | :----: |\n", + "| 重载1 | 重载2 | 重载3 |\n", + "| | dim | other |\n", + "| | keepdim | out |\n", + "| | out | |\n", + "\n", + "| jax |\n", + "| :----: |\n", + "| axis |\n", + "| out |\n", + "| keepdims |\n", + "| initial |\n", + "| where |\n" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "markdown", + "id": "4a60709b-e71a-404b-ad89-bcd906d38d1c", + "metadata": {}, + "source": [ + "重载1:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " 1\n", + "\n", + "\n", + "torch output:\n", + " tensor(1)\n", + "\n", + "\n", + "jax output:\n", + " 1\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax\n", + "\n", + "input = np.array([[9, 3, 4, 5],\n", + " [5, 2, 7, 4],\n", + " [8, 1, 3, 6]])\n", + "\n", + "y1 = ms.tensor(input)\n", + "y1_output = y1.min()\n", + "y2 = torch.tensor(input)\n", + "y2_output = y2.min()\n", + "y3_output = input.min()\n", + "print ('mindspore output:\\n',y1_output)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2_output)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3_output)" + ] + }, + { + "cell_type": "markdown", + "id": "6d461b44-93c6-43a8-a6ca-160b0b82c743", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "43e8eb29-9488-4172-8645-7ee376c94220", + "metadata": {}, + "source": [ + "重载2:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "9f3d8f1b-4539-4b5b-83c2-4221a401a60d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " (Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[3],\n", + " [2],\n", + " [1]]), Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[1],\n", + " [1],\n", + " [1]]))\n", + "\n", + "\n", + "torch output:\n", + " torch.return_types.min(\n", + "values=tensor([[3],\n", + " [2],\n", + " [1]]),\n", + "indices=tensor([[1],\n", + " [1],\n", + " [1]]))\n", + "\n", + "\n", + "jax output:\n", + " [[3]\n", + " [2]\n", + " [1]]\n" + ] + } + ], + "source": [ + "y1 = ms.tensor(input)\n", + "y1_output = y1.min(dim=1, keepdim=True)\n", + "y2 = torch.tensor(input)\n", + "y2_output = y2.min(dim=1, keepdim=True)\n", + "y3 = input\n", + "y3_output = y3.min(axis=1, keepdims=True)\n", + "print ('mindspore output:\\n',y1_output)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2_output)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3_output)" + ] + }, + { + "cell_type": "markdown", + "id": "e4fb7204-1661-4540-9441-f6ab68d967f7", + "metadata": {}, + "source": [ + "* jax不返回最大值的索引。" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "重载3:ms与jax" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "209694eb-6a58-43c1-a450-9450d2ebf09d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " (Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[0],\n", + " [0],\n", + " [0]]), Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[0],\n", + " [0],\n", + " [0]]))\n", + "\n", + "\n", + "jax output:\n", + " [[0]\n", + " [0]\n", + " [0]]\n" + ] + } + ], + "source": [ + "where=np.array([[0, 0, 0, 0],\n", + " [0, 1, 0, 1],\n", + " [1, 1, 1, 0]], dtype=bool)\n", + "\n", + "y1 = ms.tensor(input)\n", + "y1_output = y1.min(axis=1, keepdims=True, initial=0, where=ms.tensor(where), return_indices=True)\n", + "y3 = input\n", + "y3_output = y3.min(axis=1, keepdims=True, initial=0, where=ms.tensor(where))\n", + "print ('mindspore output:\\n',y1_output)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3_output)" + ] + }, + { + "cell_type": "markdown", + "id": "8a9a6196-d580-4f57-b5b7-efebefbbdc5c", + "metadata": {}, + "source": [ + "* jax不返回最大值的索引。" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "1f0ea595-beca-48ea-8154-adfb94bca0a8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch output:\n", + " tensor([[9, 0, 0, 0],\n", + " [0, 1, 0, 1],\n", + " [1, 1, 3, 0]])\n" + ] + } + ], + "source": [ + "other=np.array([[10, 0, 0, 0],\n", + " [0, 1, 0, 1],\n", + " [1, 1, 5, 0]])\n", + "y2 = torch.tensor(input)\n", + "y2_output = y2.min(torch.tensor(other))\n", + "print ('torch output:\\n',y2_output)" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling min with \"min(dim=Tuple, keepdim=bool)\".\nThe valid calling should be:\n\"Tensor.min()\"\n\"Tensor.min(dim=, keepdim=)\"\n\"Tensor.min(axis=, keepdims=, *, initial=, where=, return_indices=)\"\n\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/op_function/converter.h:112 Parse\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1_output \u001b[38;5;241m=\u001b[39m \u001b[43my1\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmin\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: Failed calling min with \"min(dim=Tuple, keepdim=bool)\".\nThe valid calling should be:\n\"Tensor.min()\"\n\"Tensor.min(dim=, keepdim=)\"\n\"Tensor.min(axis=, keepdims=, *, initial=, where=, return_indices=)\"\n\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/op_function/converter.h:112 Parse\n" + ] + } + ], + "source": [ + "y1_output = y1.min(dim=(1,2), keepdim=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "min() received an invalid combination of arguments - got (keepdim=bool, dim=tuple, ), but expected one of:\n * ()\n * (Tensor other)\n * (int dim, bool keepdim)\n didn't match because some of the arguments have invalid types: (!dim=tuple of (int, int)!, keepdim=bool, )\n * (name dim, bool keepdim)\n didn't match because some of the arguments have invalid types: (!dim=tuple of (int, int)!, keepdim=bool, )\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2_output \u001b[38;5;241m=\u001b[39m \u001b[43my2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmin\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdim\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: min() received an invalid combination of arguments - got (keepdim=bool, dim=tuple, ), but expected one of:\n * ()\n * (Tensor other)\n * (int dim, bool keepdim)\n didn't match because some of the arguments have invalid types: (!dim=tuple of (int, int)!, keepdim=bool, )\n * (name dim, bool keepdim)\n didn't match because some of the arguments have invalid types: (!dim=tuple of (int, int)!, keepdim=bool, )\n" + ] + } + ], + "source": [ + "y2_output = y2.min(dim=(1,2), keepdim=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "duplicate value in 'axis'", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[7], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y3_output \u001b[38;5;241m=\u001b[39m \u001b[43my3\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmin\u001b[49m\u001b[43m(\u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdims\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/numpy/core/_methods.py:45\u001b[0m, in \u001b[0;36m_amin\u001b[0;34m(a, axis, out, keepdims, initial, where)\u001b[0m\n\u001b[1;32m 43\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_amin\u001b[39m(a, axis\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, out\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, keepdims\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[1;32m 44\u001b[0m initial\u001b[38;5;241m=\u001b[39m_NoValue, where\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m):\n\u001b[0;32m---> 45\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mumr_minimum\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mout\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdims\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minitial\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mwhere\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mValueError\u001b[0m: duplicate value in 'axis'" + ] + } + ], + "source": [ + "y3_output = y3.min(axis=(1,1), keepdims=True)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,ms报错信息与torch对齐。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 551ac6edd9b34f825bf0543d409c6ba438c64fdb Mon Sep 17 00:00:00 2001 From: mengxian Date: Mon, 10 Feb 2025 10:46:02 +0800 Subject: [PATCH 5/5] API --- .../ops.dsplit-checkpoint.ipynb | 384 ++++++++++++++++++ .../ops.max-checkpoint.ipynb | 261 ++++++++++++ .../ops.tensor_dot-checkpoint.ipynb | 205 ++++++++++ .../ops.transpose-checkpoint.ipynb | 268 ++++++++++++ api-examples/operator/ops.dsplit.ipynb | 384 ++++++++++++++++++ api-examples/tensor/.DS_Store | Bin 0 -> 6148 bytes api-examples/tensor/tensor.max.ipynb | 170 +++++++- 7 files changed, 1653 insertions(+), 19 deletions(-) create mode 100644 api-examples/operator/.ipynb_checkpoints/ops.dsplit-checkpoint.ipynb create mode 100644 api-examples/operator/.ipynb_checkpoints/ops.max-checkpoint.ipynb create mode 100644 api-examples/operator/.ipynb_checkpoints/ops.tensor_dot-checkpoint.ipynb create mode 100644 api-examples/operator/.ipynb_checkpoints/ops.transpose-checkpoint.ipynb create mode 100644 api-examples/operator/ops.dsplit.ipynb create mode 100644 api-examples/tensor/.DS_Store diff --git a/api-examples/operator/.ipynb_checkpoints/ops.dsplit-checkpoint.ipynb b/api-examples/operator/.ipynb_checkpoints/ops.dsplit-checkpoint.ipynb new file mode 100644 index 0000000..8bec1c5 --- /dev/null +++ b/api-examples/operator/.ipynb_checkpoints/ops.dsplit-checkpoint.ipynb @@ -0,0 +1,384 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "f244a0e4-7a41-4db8-a9b6-6da4b3ed0964", + "metadata": {}, + "source": [ + "## mindspore.ops.dsplit(input, indices_or_sections) -〉 tuple[Tensor]\n", + "沿着第三轴将输入Tensor分割成多个子Tensor。等同于axis = 2时的 ops.tensor_split 。\n", + "- 输入:\n", + " * input (Tensor)\n", + " * indices_or_sections (Union[int, tuple(int), list(int)])\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "1889e83d-6e4e-419a-a308-9bb0e4b551d9", + "metadata": {}, + "source": [ + "## 1、参数比较:\n", + "| mindspore | torch |\n", + "| :----: | :----: |\n", + "| input | input |\n", + "| indices_or_sections | indices_or_sections |" + ] + }, + { + "cell_type": "markdown", + "id": "eb184c6e-9cc7-4410-b92d-228b4bc1015b", + "metadata": {}, + "source": [ + "## 2、返回值比较 \n", + "当indices_or_sections 为 int时:" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "a651e5cd-21c8-44fa-be0b-824d3e393aeb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "input:\n", + " [[[ 0. 1. 2. 3.]\n", + " [ 4. 5. 6. 7.]]\n", + "\n", + " [[ 8. 9. 10. 11.]\n", + " [12. 13. 14. 15.]]]\n", + "\n", + "\n", + "torch output:\n", + " (tensor([[[ 0., 1.],\n", + " [ 4., 5.]],\n", + "\n", + " [[ 8., 9.],\n", + " [12., 13.]]], dtype=torch.float64), tensor([[[ 2., 3.],\n", + " [ 6., 7.]],\n", + "\n", + " [[10., 11.],\n", + " [14., 15.]]], dtype=torch.float64))\n", + "\n", + "\n", + "ms output:\n", + " (Tensor(shape=[2, 2, 2], dtype=Float64, value=\n", + "[[[ 0.00000000e+00, 1.00000000e+00],\n", + " [ 4.00000000e+00, 5.00000000e+00]],\n", + " [[ 8.00000000e+00, 9.00000000e+00],\n", + " [ 1.20000000e+01, 1.30000000e+01]]]), Tensor(shape=[2, 2, 2], dtype=Float64, value=\n", + "[[[ 2.00000000e+00, 3.00000000e+00],\n", + " [ 6.00000000e+00, 7.00000000e+00]],\n", + " [[ 1.00000000e+01, 1.10000000e+01],\n", + " [ 1.40000000e+01, 1.50000000e+01]]]))\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import torch\n", + "import mindspore as ms\n", + "from mindspore import Tensor, ops\n", + "\n", + "input = np.arange(16.0).reshape(2, 2, 4)\n", + "print(\"input:\\n\", input)\n", + "print(\"\\n\")\n", + "output1 = torch.dsplit(torch.tensor(input), 2)\n", + "print(\"torch output:\\n\", output1)\n", + "print(\"\\n\")\n", + "output2 = ms.ops.dsplit(ms.tensor(input), 2)\n", + "print(\"ms output:\\n\", output2)" + ] + }, + { + "cell_type": "markdown", + "id": "e3ffb2b7-4a3d-48ac-933e-267ed55a487f", + "metadata": {}, + "source": [ + "当indices_or_sections 为list时:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "7f0098d3-f25e-4f4c-b530-a9da368a3842", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch output1:\n", + " (tensor([[[ 0., 1., 2.],\n", + " [ 4., 5., 6.]],\n", + "\n", + " [[ 8., 9., 10.],\n", + " [12., 13., 14.]]], dtype=torch.float64), tensor([[[ 3.],\n", + " [ 7.]],\n", + "\n", + " [[11.],\n", + " [15.]]], dtype=torch.float64), tensor([], size=(2, 2, 0), dtype=torch.float64))\n", + "\n", + "\n", + "ms output2:\n", + " (tensor([[[ 0., 1., 2.],\n", + " [ 4., 5., 6.]],\n", + "\n", + " [[ 8., 9., 10.],\n", + " [12., 13., 14.]]], dtype=torch.float64), tensor([[[ 3.],\n", + " [ 7.]],\n", + "\n", + " [[11.],\n", + " [15.]]], dtype=torch.float64), tensor([], size=(2, 2, 0), dtype=torch.float64))\n" + ] + } + ], + "source": [ + "output1 = torch.dsplit(torch.tensor(input), [3, 6])\n", + "print(\"torch output1:\\n\", output1)\n", + "print(\"\\n\")\n", + "output2 = ms.ops.dsplit(ms.tensor(input), [3, 6])\n", + "print(\"ms output2:\\n\", output1)" + ] + }, + { + "cell_type": "markdown", + "id": "6171dd9a-ad43-4312-aff7-ccdb24686491", + "metadata": {}, + "source": [ + "### 与tensor_split接口比较:" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "fe162c23-2033-48c5-be31-c35606ef4aed", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch output1:\n", + " (tensor([[[ 0., 1.],\n", + " [ 4., 5.]],\n", + "\n", + " [[ 8., 9.],\n", + " [12., 13.]]], dtype=torch.float64), tensor([[[ 2., 3.],\n", + " [ 6., 7.]],\n", + "\n", + " [[10., 11.],\n", + " [14., 15.]]], dtype=torch.float64))\n", + "torch output2:\n", + " (tensor([[[ 0., 1., 2.],\n", + " [ 4., 5., 6.]],\n", + "\n", + " [[ 8., 9., 10.],\n", + " [12., 13., 14.]]], dtype=torch.float64), tensor([[[ 3.],\n", + " [ 7.]],\n", + "\n", + " [[11.],\n", + " [15.]]], dtype=torch.float64), tensor([], size=(2, 2, 0), dtype=torch.float64))\n" + ] + } + ], + "source": [ + "output1 = torch.tensor_split(torch.tensor(input), 2, 2)\n", + "print(\"torch output1:\\n\", output1)\n", + "output2 = torch.tensor_split(torch.tensor(input), [3, 6], 2)\n", + "print(\"torch output2:\\n\", output2)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "a3a9196a-46b4-484a-ba52-8ed96c18aab1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ms output1:\n", + " (Tensor(shape=[2, 2, 2], dtype=Float64, value=\n", + "[[[ 0.00000000e+00, 1.00000000e+00],\n", + " [ 4.00000000e+00, 5.00000000e+00]],\n", + " [[ 8.00000000e+00, 9.00000000e+00],\n", + " [ 1.20000000e+01, 1.30000000e+01]]]), Tensor(shape=[2, 2, 2], dtype=Float64, value=\n", + "[[[ 2.00000000e+00, 3.00000000e+00],\n", + " [ 6.00000000e+00, 7.00000000e+00]],\n", + " [[ 1.00000000e+01, 1.10000000e+01],\n", + " [ 1.40000000e+01, 1.50000000e+01]]]))\n", + "ms output2:\n", + " (Tensor(shape=[2, 2, 3], dtype=Float64, value=\n", + "[[[ 0.00000000e+00, 1.00000000e+00, 2.00000000e+00],\n", + " [ 4.00000000e+00, 5.00000000e+00, 6.00000000e+00]],\n", + " [[ 8.00000000e+00, 9.00000000e+00, 1.00000000e+01],\n", + " [ 1.20000000e+01, 1.30000000e+01, 1.40000000e+01]]]), Tensor(shape=[2, 2, 1], dtype=Float64, value=\n", + "[[[ 3.00000000e+00],\n", + " [ 7.00000000e+00]],\n", + " [[ 1.10000000e+01],\n", + " [ 1.50000000e+01]]]), Tensor(shape=[2, 2, 0], dtype=Float64, value=\n", + "))\n" + ] + } + ], + "source": [ + "output1 = ms.ops.tensor_split(ms.tensor(input), 2, 2)\n", + "print(\"ms output1:\\n\", output1)\n", + "output2 = ms.ops.tensor_split(ms.tensor(input), [3, 6], 2)\n", + "print(\"ms output2:\\n\", output2)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "bd32c32a-2262-494b-a2c6-993b1326a7a6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor([[0, 1],\n", + " [2, 3],\n", + " [4, 5]]),\n", + " tensor([[6, 7],\n", + " [8, 9]]))" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = torch.arange(10).reshape(5, 2)\n", + "torch.split(a, 3)" + ] + }, + { + "cell_type": "markdown", + "id": "e71d8217-1c2f-4fde-aa01-4191cba54d9c", + "metadata": {}, + "source": [ + "当axis=2时,ms与torch的dsplit与tensor_split接口返回值一致。\n", + "但ms的两个接口输出格式有区别。" + ] + }, + { + "cell_type": "markdown", + "id": "b7a362b7-f9df-4dfe-8743-1d4089218fb3", + "metadata": {}, + "source": [ + "### 与torch.split的比较: \n", + "第二个参数含义不同:\n", + "\n", + "\n", + "\n", + " \n", + " \n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
torch.dsplit: indices_or_sectionstorch.split: split_size_or_sections
int切分的份数切分每份的大小
list在索引处进行切分切分每份的大小
\n", + "\n", + "\n", + "\n", + "所以为了得到同样的输出,入参需要改变,如下:" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "88508217-74b0-42d9-bd5a-35f6fc659018", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch output1:\n", + " (tensor([[[ 0., 1.],\n", + " [ 4., 5.]],\n", + "\n", + " [[ 8., 9.],\n", + " [12., 13.]]], dtype=torch.float64), tensor([[[ 2., 3.],\n", + " [ 6., 7.]],\n", + "\n", + " [[10., 11.],\n", + " [14., 15.]]], dtype=torch.float64))\n", + "torch output2:\n", + " (tensor([[[ 0., 1., 2.],\n", + " [ 4., 5., 6.]],\n", + "\n", + " [[ 8., 9., 10.],\n", + " [12., 13., 14.]]], dtype=torch.float64), tensor([[[ 3.],\n", + " [ 7.]],\n", + "\n", + " [[11.],\n", + " [15.]]], dtype=torch.float64))\n" + ] + } + ], + "source": [ + "output1 = torch.split(torch.tensor(input), 2, 2)\n", + "print(\"torch output1:\\n\", output1)\n", + "output2 = torch.split(torch.tensor(input), [3, 1], 2)\n", + "print(\"torch output2:\\n\", output2)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/.ipynb_checkpoints/ops.max-checkpoint.ipynb b/api-examples/operator/.ipynb_checkpoints/ops.max-checkpoint.ipynb new file mode 100644 index 0000000..b3082b6 --- /dev/null +++ b/api-examples/operator/.ipynb_checkpoints/ops.max-checkpoint.ipynb @@ -0,0 +1,261 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.max(input, axis=None, keepdims=False, *, initial=None, where=None) -〉 tuple (Tensor)\n", + "返回给定轴上的最大值及其索引。\n", + "- 输入:\n", + " * input: mindspore的tensor。\n", + " * axis: int。\n", + " * keepdims: bool。\n", + " * initial: scalar\n", + " * where:Tensor[bool]\n", + "\n", + "- 返回:2个mindspore的tensor组成的tuple, 最大值及最大值的索引。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | a |\n", + "| axis | dim | axis |\n", + "| keepdims | keepdim | keepdims |\n", + "| initial | out | initial |\n", + "| where | | where |\n", + "| | | out |\n", + "\n", + "\n", + "* torch和jax都提供了out出参方式\n", + "* torch没有initial和where参数,分别表示返回值的初始值及是否要计算最大值。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " (Tensor(shape=[3], dtype=Int64, value= [9, 7, 8]), Tensor(shape=[3], dtype=Int64, value= [0, 2, 0]))\n", + "\n", + "\n", + "torch output:\n", + " torch.return_types.max(\n", + "values=tensor([9, 7, 8]),\n", + "indices=tensor([0, 2, 0]))\n", + "\n", + "\n", + "jax output:\n", + " [9 7 8]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[9, 3, 4, 5],\n", + " [5, 2, 7, 4],\n", + " [8, 1, 3, 6]])\n", + "\n", + "y1 = ms.ops.max(ms.tensor(input), 1)\n", + "y2 = torch.max(torch.tensor(input), 1)\n", + "y3 = jnp.max(input, 1)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms返回的信息不简洁,关键信息value在最后面,可读性差。 \n", + "* jax仅返回最大值,不返回最大值索引。" + ] + }, + { + "cell_type": "markdown", + "id": "55e2ba4f-bea6-47f6-9176-c191cc994bd6", + "metadata": {}, + "source": [ + "使用where,来确定计算范围时:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "684b8ee0-7ad8-493d-a459-646cfe8b9fdd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " (Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[0],\n", + " [4],\n", + " [8]]), Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[0],\n", + " [3],\n", + " [0]]))\n", + "\n", + "\n", + "jax output:\n", + " [[0]\n", + " [4]\n", + " [8]]\n" + ] + } + ], + "source": [ + "where=np.array([[0, 0, 0, 0],\n", + " [0, 1, 0, 1],\n", + " [1, 1, 1, 0]], dtype=bool)\n", + "\n", + "y1 = ms.ops.max(ms.tensor(input), 1, keepdims=True, initial=0, where=ms.tensor(where))\n", + "y3 = jnp.max(input, 1, keepdims=True, initial=0, where=where)\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling ArgMaxWithValue with \"ArgMaxWithValue(axis=int, keep_dims=bool)(input=)\".\nThe valid calling should be: \n\"ArgMaxWithValue(axis=, keep_dims=)(input=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/wy/5yzfdb6x7pvfxh89zcmf_w780000gn/T/ipykernel_83845/3848010251.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0my1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmax\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/array_func.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(input, axis, keepdims, initial, where)\u001b[0m\n\u001b[1;32m 5447\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0maxis\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mint\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5448\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"For 'max', 'axis' must be int, but got {type(axis)}\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5449\u001b[0m \u001b[0minput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_init_and_select_elem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minitial\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwhere\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmaximum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5450\u001b[0m \u001b[0margmax_with_value_op\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_get_cache_prim\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mArgMaxWithValue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 5451\u001b[0;31m \u001b[0mindices\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalues\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0margmax_with_value_op\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5452\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindices\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(self, input)\u001b[0m\n\u001b[1;32m 1673\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1674\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_convert_stub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpyboost_argmax_with_value\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mkeep_dims\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: Failed calling ArgMaxWithValue with \"ArgMaxWithValue(axis=int, keep_dims=bool)(input=)\".\nThe valid calling should be: \n\"ArgMaxWithValue(axis=, keep_dims=)(input=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.max(input, 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "max() received an invalid combination of arguments - got (numpy.ndarray, int), but expected one of:\n * (Tensor input, *, Tensor out)\n * (Tensor input, Tensor other, *, Tensor out)\n * (Tensor input, int dim, bool keepdim, *, tuple of Tensors out)\n * (Tensor input, name dim, bool keepdim, *, tuple of Tensors out)\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: max() received an invalid combination of arguments - got (numpy.ndarray, int), but expected one of:\n * (Tensor input, *, Tensor out)\n * (Tensor input, Tensor other, *, Tensor out)\n * (Tensor input, int dim, bool keepdim, *, tuple of Tensors out)\n * (Tensor input, name dim, bool keepdim, *, tuple of Tensors out)\n" + ] + } + ], + "source": [ + "y2 = torch.max(input, 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "max requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[11], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/reductions.py:268\u001b[0m, in \u001b[0;36mmax\u001b[0;34m(a, axis, out, keepdims, initial, where)\u001b[0m\n\u001b[1;32m 264\u001b[0m \u001b[38;5;129m@implements\u001b[39m(np\u001b[38;5;241m.\u001b[39mmax, skip_params\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mout\u001b[39m\u001b[38;5;124m'\u001b[39m])\n\u001b[1;32m 265\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmax\u001b[39m(a: ArrayLike, axis: Axis \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m, out: \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 266\u001b[0m keepdims: \u001b[38;5;28mbool\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m, initial: ArrayLike \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 267\u001b[0m where: ArrayLike \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m--> 268\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_reduce_max\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m_ensure_optional_axes\u001b[49m\u001b[43m(\u001b[49m\u001b[43maxis\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mout\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 269\u001b[0m \u001b[43m \u001b[49m\u001b[43mkeepdims\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkeepdims\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minitial\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minitial\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mwhere\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwhere\u001b[49m\u001b[43m)\u001b[49m\n", + " \u001b[0;31m[... skipping hidden 11 frame]\u001b[0m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/reductions.py:260\u001b[0m, in \u001b[0;36m_reduce_max\u001b[0;34m(a, axis, out, keepdims, initial, where)\u001b[0m\n\u001b[1;32m 256\u001b[0m \u001b[38;5;129m@partial\u001b[39m(api\u001b[38;5;241m.\u001b[39mjit, static_argnames\u001b[38;5;241m=\u001b[39m(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124maxis\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mkeepdims\u001b[39m\u001b[38;5;124m'\u001b[39m), inline\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m)\n\u001b[1;32m 257\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_reduce_max\u001b[39m(a: ArrayLike, axis: Axis \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m, out: \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 258\u001b[0m keepdims: \u001b[38;5;28mbool\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m, initial: ArrayLike \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 259\u001b[0m where: ArrayLike \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[0;32m--> 260\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_reduction\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mmax\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mlax\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43minf\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mhas_identity\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 261\u001b[0m \u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mout\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdims\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mkeepdims\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 262\u001b[0m \u001b[43m \u001b[49m\u001b[43minitial\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minitial\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mwhere_\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwhere\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparallel_reduce\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlax\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpmax\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/reductions.py:100\u001b[0m, in \u001b[0;36m_reduction\u001b[0;34m(a, name, np_fun, op, init_val, has_identity, preproc, bool_op, upcast_f16_for_computation, axis, dtype, out, keepdims, initial, where_, parallel_reduce, promote_integers)\u001b[0m\n\u001b[1;32m 98\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m out \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 99\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mNotImplementedError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mThe \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mout\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m argument to jnp.\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m is not supported.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 100\u001b[0m \u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[43mname\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 101\u001b[0m dtypes\u001b[38;5;241m.\u001b[39mcheck_user_dtype_supported(dtype, name)\n\u001b[1;32m 102\u001b[0m axis \u001b[38;5;241m=\u001b[39m core\u001b[38;5;241m.\u001b[39mconcrete_or_error(\u001b[38;5;28;01mNone\u001b[39;00m, axis, \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124maxis argument to jnp.\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m().\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: max requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.max(input, 1)" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/.ipynb_checkpoints/ops.tensor_dot-checkpoint.ipynb b/api-examples/operator/.ipynb_checkpoints/ops.tensor_dot-checkpoint.ipynb new file mode 100644 index 0000000..36eb890 --- /dev/null +++ b/api-examples/operator/.ipynb_checkpoints/ops.tensor_dot-checkpoint.ipynb @@ -0,0 +1,205 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.tensor_dot(x1, x2, axes) -〉 Tensor\n", + "在指定轴上对两个输入进行点乘操作。\n", + "- 输入:\n", + " * x1:mindspore的tensor, 数据类型为float16或float32。\n", + " * x2:mindspore的tensor, 数据类型为float16或float32。\n", + " * axes:int, tuple(int), tuple(tuple(int)), list(list(int))。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| x1 | a | a |\n", + "| x2 | b | b |\n", + "| axes | dims | axes |\n", + "| | out | precision |\n", + "| | | preferred_element_type |\n", + "* torch额外提供了out出参\n", + "* jax额外提供了precision用于设置后端精度,以及preferred_element_type设置返回值的数据类型。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[ 66. 66. 66. 66. 66.]\n", + " [210. 210. 210. 210. 210.]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[ 66., 66., 66., 66., 66.],\n", + " [210., 210., 210., 210., 210.]], dtype=torch.float64)\n", + "\n", + "\n", + "jax output:\n", + " [[ 66. 66. 66. 66. 66.]\n", + " [210. 210. 210. 210. 210.]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "x1 = np.arange(24.).reshape(2, 3, 4)\n", + "x2 = np.ones((3, 4, 5))\n", + "\n", + "y1 = ms.ops.tensor_dot(ms.tensor(x1), ms.tensor(x2), axes=([1, 2], [0, 1]))\n", + "y2 = torch.tensordot(torch.tensor(x1), torch.tensor(x2), dims=([1, 2], [0, 1]))\n", + "y3 = jnp.tensordot(x1, x2, axes=([1, 2], [0, 1]))\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "For primitive[Shape], the input argument must be Tensor, but got .", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y1 \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensor_dot\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx1\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx2\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxes\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/function/math_func.py:11460\u001b[0m, in \u001b[0;36mtensor_dot\u001b[0;34m(x1, x2, axes)\u001b[0m\n\u001b[1;32m 11458\u001b[0m matmul_op \u001b[38;5;241m=\u001b[39m _get_cache_prim(P\u001b[38;5;241m.\u001b[39mMatMul)(\u001b[38;5;28;01mFalse\u001b[39;00m, \u001b[38;5;28;01mFalse\u001b[39;00m)\n\u001b[1;32m 11459\u001b[0m \u001b[38;5;66;03m# input validity checks\u001b[39;00m\n\u001b[0;32m> 11460\u001b[0m x1_shape \u001b[38;5;241m=\u001b[39m \u001b[43mshape_\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx1\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 11461\u001b[0m x2_shape \u001b[38;5;241m=\u001b[39m shape_(x2)\n\u001b[1;32m 11462\u001b[0m axes \u001b[38;5;241m=\u001b[39m _check_axes(axes, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtensor_dot\u001b[39m\u001b[38;5;124m'\u001b[39m)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/operations/manually_defined/ops_def.py:864\u001b[0m, in \u001b[0;36mShape.__call__\u001b[0;34m(self, x)\u001b[0m\n\u001b[1;32m 862\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(x, (Tensor, COOTensor, CSRTensor, Tensor_)):\n\u001b[1;32m 863\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m x\u001b[38;5;241m.\u001b[39mshape\n\u001b[0;32m--> 864\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFor primitive[\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mname\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m], the input argument must be Tensor, but got \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mtype\u001b[39m(x)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n", + "\u001b[0;31mTypeError\u001b[0m: For primitive[Shape], the input argument must be Tensor, but got ." + ] + } + ], + "source": [ + "y1 = ms.ops.tensor_dot(x1, x2, axes=([1, 2], [0, 1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "tensordot(): argument 'input' (position 1) must be Tensor, not numpy.ndarray", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensordot\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx1\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx2\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdims\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/torch/functional.py:1213\u001b[0m, in \u001b[0;36mtensordot\u001b[0;34m(a, b, dims, out)\u001b[0m\n\u001b[1;32m 1210\u001b[0m dims_b \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(\u001b[38;5;28mrange\u001b[39m(dims))\n\u001b[1;32m 1212\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m out \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 1213\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_VF\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensordot\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdims_a\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdims_b\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;66;03m# type: ignore[attr-defined]\u001b[39;00m\n\u001b[1;32m 1214\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 1215\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m _VF\u001b[38;5;241m.\u001b[39mtensordot(a, b, dims_a, dims_b, out\u001b[38;5;241m=\u001b[39mout)\n", + "\u001b[0;31mTypeError\u001b[0m: tensordot(): argument 'input' (position 1) must be Tensor, not numpy.ndarray" + ] + } + ], + "source": [ + "y2 = torch.tensordot(x1, x2, dims=([1, 2], [0, 1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "tensordot requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[6], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensordot\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mx2\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxes\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:4879\u001b[0m, in \u001b[0;36mtensordot\u001b[0;34m(a, b, axes, precision, preferred_element_type)\u001b[0m\n\u001b[1;32m 4804\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtensordot\u001b[39m(a: ArrayLike, b: ArrayLike,\n\u001b[1;32m 4805\u001b[0m axes: \u001b[38;5;28mint\u001b[39m \u001b[38;5;241m|\u001b[39m Sequence[\u001b[38;5;28mint\u001b[39m] \u001b[38;5;241m|\u001b[39m Sequence[Sequence[\u001b[38;5;28mint\u001b[39m]] \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m2\u001b[39m,\n\u001b[1;32m 4806\u001b[0m \u001b[38;5;241m*\u001b[39m, precision: PrecisionLike \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m 4807\u001b[0m preferred_element_type: DTypeLike \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[1;32m 4808\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Compute the tensor dot product of two N-dimensional arrays.\u001b[39;00m\n\u001b[1;32m 4809\u001b[0m \n\u001b[1;32m 4810\u001b[0m \u001b[38;5;124;03m JAX implementation of :func:`numpy.linalg.tensordot`.\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 4877\u001b[0m \u001b[38;5;124;03m [2, 4, 6]], dtype=int32)\u001b[39;00m\n\u001b[1;32m 4878\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 4879\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtensordot\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mb\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 4880\u001b[0m dtypes\u001b[38;5;241m.\u001b[39mcheck_user_dtype_supported(preferred_element_type, \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtensordot\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 4881\u001b[0m a, b \u001b[38;5;241m=\u001b[39m asarray(a), asarray(b)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: tensordot requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.tensordot(input, x2, axes=([1, 2], [0, 1]))" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/.ipynb_checkpoints/ops.transpose-checkpoint.ipynb b/api-examples/operator/.ipynb_checkpoints/ops.transpose-checkpoint.ipynb new file mode 100644 index 0000000..a0ee4eb --- /dev/null +++ b/api-examples/operator/.ipynb_checkpoints/ops.transpose-checkpoint.ipynb @@ -0,0 +1,268 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "620b1bb9-d188-4916-8a88-392b301e78dd", + "metadata": {}, + "source": [ + "## mindspore.ops.transpose(input, input_perm) -〉 Tensor\n", + "按指定的排列对输入进行数据重排。\n", + "- 输入:\n", + " * input:mindspore的tensor。\n", + " * input_perm:tuple(int)。\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "a73e925e-7e1d-4e81-810f-fd251bfcd7ac", + "metadata": {}, + "source": [ + "1、参数比较:\n", + "| mindspore | torch | jax |\n", + "| :----: | :----: | :----: |\n", + "| input | input | a |\n", + "| input_perm | dim0 | axes |\n", + "| | dim1 | |\n", + "\n", + "* ms与jax相同,使用第二个参数来设置按什么维度顺序进行重排。torch使用第二、三个参数设置按哪个维度进行重排。" + ] + }, + { + "cell_type": "markdown", + "id": "77ff63bc-9e39-4849-85b1-a34b7e49aee4", + "metadata": {}, + "source": [ + "2、返回值比较" + ] + }, + { + "cell_type": "markdown", + "id": "6bd9bdf0-bc8b-4fe3-9e2f-c9e032f19f67", + "metadata": {}, + "source": [ + "未设定重排维度情况下,默认值比较:" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "518c829d-80a5-4d98-b90c-09635ae35665", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "jax output:\n", + " [[[ 1 7]\n", + " [ 4 10]]\n", + "\n", + " [[ 2 8]\n", + " [ 5 11]]\n", + "\n", + " [[ 3 9]\n", + " [ 6 12]]]\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import mindspore as ms\n", + "import torch\n", + "import jax.numpy as jnp\n", + "\n", + "input = np.array([[[1, 2, 3], \n", + " [4, 5, 6]], \n", + " [[7, 8, 9], \n", + " [10, 11, 12]]])\n", + "\n", + "# y1 = ms.ops.transpose(ms.tensor(input))\n", + "# y2 = torch.transpose(torch.tensor(input))\n", + "y3 = jnp.transpose(input)\n", + "# print ('mindspore output:\\n',y1)\n", + "# print('\\n')\n", + "# print ('torch output:\\n',y2)\n", + "# print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "7e425850-50eb-4ac9-8f02-728ea64225a7", + "metadata": {}, + "source": [ + "* ms与torch的input_perm、dim0和dim1均为必选参数必须设置。\n", + "* jax的默认行为是按将整个维度倒序排列,如上述例子,等同于设置axes=(2, 1, 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "50e41829-6783-4032-bd5d-867b97b1dc18", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " [[[ 1 4]\n", + " [ 2 5]\n", + " [ 3 6]]\n", + "\n", + " [[ 7 10]\n", + " [ 8 11]\n", + " [ 9 12]]]\n", + "\n", + "\n", + "torch output:\n", + " tensor([[[ 1, 4],\n", + " [ 2, 5],\n", + " [ 3, 6]],\n", + "\n", + " [[ 7, 10],\n", + " [ 8, 11],\n", + " [ 9, 12]]])\n", + "\n", + "\n", + "jax output:\n", + " [[[ 1 4]\n", + " [ 2 5]\n", + " [ 3 6]]\n", + "\n", + " [[ 7 10]\n", + " [ 8 11]\n", + " [ 9 12]]]\n" + ] + } + ], + "source": [ + "y1 = ms.ops.transpose(ms.tensor(input), (0, 2, 1))\n", + "y2 = torch.transpose(torch.tensor(input), dim0=2, dim1=1)\n", + "y3 = jnp.transpose(input, (0, 2, 1))\n", + "print ('mindspore output:\\n',y1)\n", + "print('\\n')\n", + "print ('torch output:\\n',y2)\n", + "print('\\n')\n", + "print ('jax output:\\n',y3)" + ] + }, + { + "cell_type": "markdown", + "id": "d068b3ce-1451-48ff-917b-c40263f0ebee", + "metadata": {}, + "source": [ + "* ms与jax不返回类型。" + ] + }, + { + "cell_type": "markdown", + "id": "bc9f57fe-a2e6-4918-9e5f-ede7543cea00", + "metadata": {}, + "source": [ + "3、报错信息比较" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "9fcbe1fa-7af2-4f85-9c27-c318cb2e7017", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "Failed calling Transpose with \"Transpose()(input=, input_perm=Tuple)\".\nThe valid calling should be: \n\"Transpose()(input=, input_perm=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m/var/folders/wy/5yzfdb6x7pvfxh89zcmf_w780000gn/T/ipykernel_18001/1707311972.py\u001b[0m in \u001b[0;36m?\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0my1\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtranspose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_def.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(input, input_perm)\u001b[0m\n\u001b[1;32m 8051\u001b[0m [[ 7. 10.]\n\u001b[1;32m 8052\u001b[0m \u001b[0;34m[\u001b[0m \u001b[0;36m8.\u001b[0m \u001b[0;36m11.\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8053\u001b[0m [ 9. 12.]]]\n\u001b[1;32m 8054\u001b[0m \"\"\"\n\u001b[0;32m-> 8055\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mtranspose_op\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput_perm\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/auto_generate/gen_ops_prim.py\u001b[0m in \u001b[0;36m?\u001b[0;34m(self, input, input_perm)\u001b[0m\n\u001b[1;32m 15142\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__call__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput_perm\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m> 15143\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0m_convert_stub\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpyboost_transpose\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0minput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput_perm\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m: Failed calling Transpose with \"Transpose()(input=, input_perm=Tuple)\".\nThe valid calling should be: \n\"Transpose()(input=, input_perm=)\".\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/pynative_utils.cc:1294 PrintTypeCastError\n" + ] + } + ], + "source": [ + "y1 = ms.ops.transpose(input, (0, 2, 1))" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "1050b2d7-b6e4-4cce-90ed-9797c0dba5e9", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "transpose() received an invalid combination of arguments - got (numpy.ndarray, tuple), but expected one of:\n * (Tensor input, int dim0, int dim1)\n * (Tensor input, name dim0, name dim1)\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[14], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m y2 \u001b[38;5;241m=\u001b[39m \u001b[43mtorch\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtranspose\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: transpose() received an invalid combination of arguments - got (numpy.ndarray, tuple), but expected one of:\n * (Tensor input, int dim0, int dim1)\n * (Tensor input, name dim0, name dim1)\n" + ] + } + ], + "source": [ + "y2 = torch.transpose(input, (0, 2, 1))" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "6697f596-9c94-4151-a577-08c8f80e6e7e", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "transpose requires ndarray or scalar arguments, got at position 0.", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[15], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28minput\u001b[39m \u001b[38;5;241m=\u001b[39m [\u001b[38;5;241m2\u001b[39m, \u001b[38;5;241m4\u001b[39m, \u001b[38;5;241m3\u001b[39m, \u001b[38;5;241m1\u001b[39m]\n\u001b[0;32m----> 2\u001b[0m y3 \u001b[38;5;241m=\u001b[39m \u001b[43mjnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtranspose\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/lax_numpy.py:765\u001b[0m, in \u001b[0;36mtranspose\u001b[0;34m(a, axes)\u001b[0m\n\u001b[1;32m 693\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mtranspose\u001b[39m(a: ArrayLike, axes: Sequence[\u001b[38;5;28mint\u001b[39m] \u001b[38;5;241m|\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Array:\n\u001b[1;32m 694\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Return a transposed version of an N-dimensional array.\u001b[39;00m\n\u001b[1;32m 695\u001b[0m \n\u001b[1;32m 696\u001b[0m \u001b[38;5;124;03m JAX implementation of :func:`numpy.transpose`, implemented in terms of\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 763\u001b[0m \u001b[38;5;124;03m [2, 4]], dtype=int32)\u001b[39;00m\n\u001b[1;32m 764\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 765\u001b[0m \u001b[43mutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcheck_arraylike\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mtranspose\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43ma\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 766\u001b[0m axes_ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mlist\u001b[39m(\u001b[38;5;28mrange\u001b[39m(ndim(a))[::\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m]) \u001b[38;5;28;01mif\u001b[39;00m axes \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m \u001b[38;5;28;01melse\u001b[39;00m axes\n\u001b[1;32m 767\u001b[0m axes_ \u001b[38;5;241m=\u001b[39m [_canonicalize_axis(i, ndim(a)) \u001b[38;5;28;01mfor\u001b[39;00m i \u001b[38;5;129;01min\u001b[39;00m axes_]\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/jax/_src/numpy/util.py:335\u001b[0m, in \u001b[0;36mcheck_arraylike\u001b[0;34m(fun_name, emit_warning, stacklevel, *args)\u001b[0m\n\u001b[1;32m 332\u001b[0m warnings\u001b[38;5;241m.\u001b[39mwarn(msg \u001b[38;5;241m+\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m In a future JAX release this will be an error.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 333\u001b[0m category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mDeprecationWarning\u001b[39;00m, stacklevel\u001b[38;5;241m=\u001b[39mstacklevel)\n\u001b[1;32m 334\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 335\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(msg\u001b[38;5;241m.\u001b[39mformat(fun_name, \u001b[38;5;28mtype\u001b[39m(arg), pos))\n", + "\u001b[0;31mTypeError\u001b[0m: transpose requires ndarray or scalar arguments, got at position 0." + ] + } + ], + "source": [ + "input = [2, 4, 3, 1]\n", + "y3 = jnp.transpose(input, (0, 2, 1))" + ] + }, + { + "cell_type": "markdown", + "id": "59517714-1ef1-43d2-ac0e-7b84d4ab1c65", + "metadata": {}, + "source": [ + "当输入类型不正确时,报错信息torch简洁明确。建议ms优化。" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/operator/ops.dsplit.ipynb b/api-examples/operator/ops.dsplit.ipynb new file mode 100644 index 0000000..8bec1c5 --- /dev/null +++ b/api-examples/operator/ops.dsplit.ipynb @@ -0,0 +1,384 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "f244a0e4-7a41-4db8-a9b6-6da4b3ed0964", + "metadata": {}, + "source": [ + "## mindspore.ops.dsplit(input, indices_or_sections) -〉 tuple[Tensor]\n", + "沿着第三轴将输入Tensor分割成多个子Tensor。等同于axis = 2时的 ops.tensor_split 。\n", + "- 输入:\n", + " * input (Tensor)\n", + " * indices_or_sections (Union[int, tuple(int), list(int)])\n", + "- 返回:mindspore的tensor。" + ] + }, + { + "cell_type": "markdown", + "id": "1889e83d-6e4e-419a-a308-9bb0e4b551d9", + "metadata": {}, + "source": [ + "## 1、参数比较:\n", + "| mindspore | torch |\n", + "| :----: | :----: |\n", + "| input | input |\n", + "| indices_or_sections | indices_or_sections |" + ] + }, + { + "cell_type": "markdown", + "id": "eb184c6e-9cc7-4410-b92d-228b4bc1015b", + "metadata": {}, + "source": [ + "## 2、返回值比较 \n", + "当indices_or_sections 为 int时:" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "a651e5cd-21c8-44fa-be0b-824d3e393aeb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "input:\n", + " [[[ 0. 1. 2. 3.]\n", + " [ 4. 5. 6. 7.]]\n", + "\n", + " [[ 8. 9. 10. 11.]\n", + " [12. 13. 14. 15.]]]\n", + "\n", + "\n", + "torch output:\n", + " (tensor([[[ 0., 1.],\n", + " [ 4., 5.]],\n", + "\n", + " [[ 8., 9.],\n", + " [12., 13.]]], dtype=torch.float64), tensor([[[ 2., 3.],\n", + " [ 6., 7.]],\n", + "\n", + " [[10., 11.],\n", + " [14., 15.]]], dtype=torch.float64))\n", + "\n", + "\n", + "ms output:\n", + " (Tensor(shape=[2, 2, 2], dtype=Float64, value=\n", + "[[[ 0.00000000e+00, 1.00000000e+00],\n", + " [ 4.00000000e+00, 5.00000000e+00]],\n", + " [[ 8.00000000e+00, 9.00000000e+00],\n", + " [ 1.20000000e+01, 1.30000000e+01]]]), Tensor(shape=[2, 2, 2], dtype=Float64, value=\n", + "[[[ 2.00000000e+00, 3.00000000e+00],\n", + " [ 6.00000000e+00, 7.00000000e+00]],\n", + " [[ 1.00000000e+01, 1.10000000e+01],\n", + " [ 1.40000000e+01, 1.50000000e+01]]]))\n" + ] + } + ], + "source": [ + "import numpy as np\n", + "import torch\n", + "import mindspore as ms\n", + "from mindspore import Tensor, ops\n", + "\n", + "input = np.arange(16.0).reshape(2, 2, 4)\n", + "print(\"input:\\n\", input)\n", + "print(\"\\n\")\n", + "output1 = torch.dsplit(torch.tensor(input), 2)\n", + "print(\"torch output:\\n\", output1)\n", + "print(\"\\n\")\n", + "output2 = ms.ops.dsplit(ms.tensor(input), 2)\n", + "print(\"ms output:\\n\", output2)" + ] + }, + { + "cell_type": "markdown", + "id": "e3ffb2b7-4a3d-48ac-933e-267ed55a487f", + "metadata": {}, + "source": [ + "当indices_or_sections 为list时:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "7f0098d3-f25e-4f4c-b530-a9da368a3842", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch output1:\n", + " (tensor([[[ 0., 1., 2.],\n", + " [ 4., 5., 6.]],\n", + "\n", + " [[ 8., 9., 10.],\n", + " [12., 13., 14.]]], dtype=torch.float64), tensor([[[ 3.],\n", + " [ 7.]],\n", + "\n", + " [[11.],\n", + " [15.]]], dtype=torch.float64), tensor([], size=(2, 2, 0), dtype=torch.float64))\n", + "\n", + "\n", + "ms output2:\n", + " (tensor([[[ 0., 1., 2.],\n", + " [ 4., 5., 6.]],\n", + "\n", + " [[ 8., 9., 10.],\n", + " [12., 13., 14.]]], dtype=torch.float64), tensor([[[ 3.],\n", + " [ 7.]],\n", + "\n", + " [[11.],\n", + " [15.]]], dtype=torch.float64), tensor([], size=(2, 2, 0), dtype=torch.float64))\n" + ] + } + ], + "source": [ + "output1 = torch.dsplit(torch.tensor(input), [3, 6])\n", + "print(\"torch output1:\\n\", output1)\n", + "print(\"\\n\")\n", + "output2 = ms.ops.dsplit(ms.tensor(input), [3, 6])\n", + "print(\"ms output2:\\n\", output1)" + ] + }, + { + "cell_type": "markdown", + "id": "6171dd9a-ad43-4312-aff7-ccdb24686491", + "metadata": {}, + "source": [ + "### 与tensor_split接口比较:" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "fe162c23-2033-48c5-be31-c35606ef4aed", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch output1:\n", + " (tensor([[[ 0., 1.],\n", + " [ 4., 5.]],\n", + "\n", + " [[ 8., 9.],\n", + " [12., 13.]]], dtype=torch.float64), tensor([[[ 2., 3.],\n", + " [ 6., 7.]],\n", + "\n", + " [[10., 11.],\n", + " [14., 15.]]], dtype=torch.float64))\n", + "torch output2:\n", + " (tensor([[[ 0., 1., 2.],\n", + " [ 4., 5., 6.]],\n", + "\n", + " [[ 8., 9., 10.],\n", + " [12., 13., 14.]]], dtype=torch.float64), tensor([[[ 3.],\n", + " [ 7.]],\n", + "\n", + " [[11.],\n", + " [15.]]], dtype=torch.float64), tensor([], size=(2, 2, 0), dtype=torch.float64))\n" + ] + } + ], + "source": [ + "output1 = torch.tensor_split(torch.tensor(input), 2, 2)\n", + "print(\"torch output1:\\n\", output1)\n", + "output2 = torch.tensor_split(torch.tensor(input), [3, 6], 2)\n", + "print(\"torch output2:\\n\", output2)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "a3a9196a-46b4-484a-ba52-8ed96c18aab1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "ms output1:\n", + " (Tensor(shape=[2, 2, 2], dtype=Float64, value=\n", + "[[[ 0.00000000e+00, 1.00000000e+00],\n", + " [ 4.00000000e+00, 5.00000000e+00]],\n", + " [[ 8.00000000e+00, 9.00000000e+00],\n", + " [ 1.20000000e+01, 1.30000000e+01]]]), Tensor(shape=[2, 2, 2], dtype=Float64, value=\n", + "[[[ 2.00000000e+00, 3.00000000e+00],\n", + " [ 6.00000000e+00, 7.00000000e+00]],\n", + " [[ 1.00000000e+01, 1.10000000e+01],\n", + " [ 1.40000000e+01, 1.50000000e+01]]]))\n", + "ms output2:\n", + " (Tensor(shape=[2, 2, 3], dtype=Float64, value=\n", + "[[[ 0.00000000e+00, 1.00000000e+00, 2.00000000e+00],\n", + " [ 4.00000000e+00, 5.00000000e+00, 6.00000000e+00]],\n", + " [[ 8.00000000e+00, 9.00000000e+00, 1.00000000e+01],\n", + " [ 1.20000000e+01, 1.30000000e+01, 1.40000000e+01]]]), Tensor(shape=[2, 2, 1], dtype=Float64, value=\n", + "[[[ 3.00000000e+00],\n", + " [ 7.00000000e+00]],\n", + " [[ 1.10000000e+01],\n", + " [ 1.50000000e+01]]]), Tensor(shape=[2, 2, 0], dtype=Float64, value=\n", + "))\n" + ] + } + ], + "source": [ + "output1 = ms.ops.tensor_split(ms.tensor(input), 2, 2)\n", + "print(\"ms output1:\\n\", output1)\n", + "output2 = ms.ops.tensor_split(ms.tensor(input), [3, 6], 2)\n", + "print(\"ms output2:\\n\", output2)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "id": "bd32c32a-2262-494b-a2c6-993b1326a7a6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(tensor([[0, 1],\n", + " [2, 3],\n", + " [4, 5]]),\n", + " tensor([[6, 7],\n", + " [8, 9]]))" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a = torch.arange(10).reshape(5, 2)\n", + "torch.split(a, 3)" + ] + }, + { + "cell_type": "markdown", + "id": "e71d8217-1c2f-4fde-aa01-4191cba54d9c", + "metadata": {}, + "source": [ + "当axis=2时,ms与torch的dsplit与tensor_split接口返回值一致。\n", + "但ms的两个接口输出格式有区别。" + ] + }, + { + "cell_type": "markdown", + "id": "b7a362b7-f9df-4dfe-8743-1d4089218fb3", + "metadata": {}, + "source": [ + "### 与torch.split的比较: \n", + "第二个参数含义不同:\n", + "\n", + "\n", + "\n", + " \n", + " \n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
torch.dsplit: indices_or_sectionstorch.split: split_size_or_sections
int切分的份数切分每份的大小
list在索引处进行切分切分每份的大小
\n", + "\n", + "\n", + "\n", + "所以为了得到同样的输出,入参需要改变,如下:" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "88508217-74b0-42d9-bd5a-35f6fc659018", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch output1:\n", + " (tensor([[[ 0., 1.],\n", + " [ 4., 5.]],\n", + "\n", + " [[ 8., 9.],\n", + " [12., 13.]]], dtype=torch.float64), tensor([[[ 2., 3.],\n", + " [ 6., 7.]],\n", + "\n", + " [[10., 11.],\n", + " [14., 15.]]], dtype=torch.float64))\n", + "torch output2:\n", + " (tensor([[[ 0., 1., 2.],\n", + " [ 4., 5., 6.]],\n", + "\n", + " [[ 8., 9., 10.],\n", + " [12., 13., 14.]]], dtype=torch.float64), tensor([[[ 3.],\n", + " [ 7.]],\n", + "\n", + " [[11.],\n", + " [15.]]], dtype=torch.float64))\n" + ] + } + ], + "source": [ + "output1 = torch.split(torch.tensor(input), 2, 2)\n", + "print(\"torch output1:\\n\", output1)\n", + "output2 = torch.split(torch.tensor(input), [3, 1], 2)\n", + "print(\"torch output2:\\n\", output2)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.18" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/api-examples/tensor/.DS_Store b/api-examples/tensor/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..0f015a3a251ed44e56596a808fece578f22c7ce5 GIT binary patch literal 6148 zcmeHK!Ab)`41K9RRJ<&nya@9J1^-|z_2AhLPU{OUKrbD-^L%qJ@gL7-HeHC-*BITSE(nc=I8?vv`Lhd3WYd z*&R|E#uy9)1Dg!&*khsf|K#ibe={haf`MS*Pcb0F>DhF`BYAD@9ZqX)qFzu{)UP#M jhj5ZhF@3cZA5gQ 9\u001b[0m y1_output \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mops\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax\u001b[49m\u001b[43m(\u001b[49m\u001b[43my1\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mother\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mother\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmindspore output:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m,y1_output)\n", + "\u001b[0;31mTypeError\u001b[0m: max() got an unexpected keyword argument 'other'" + ] + } + ], + "source": [ + "y1_output = ms.ops.max(y1)\n", + "print ('mindspore output:\\n',y1_output)\n", + "y1_output = ms.ops.max(y1, 1, True)\n", + "print ('mindspore output:\\n',y1_output)\n", + "\n", + "other = ms.tensor([[0, 0, 0, 0],\n", + " [0, 1, 0, 1],\n", + " [1, 1, 1, 0]])\n", + "y1_output = ms.ops.max(y1, other=other)\n", + "print ('mindspore output:\\n',y1_output)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "f17aa82a-a5c2-42fd-8e5f-3b6ec7b24e93", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "mindspore output:\n", + " 9\n", + "mindspore output:\n", + " (Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[9],\n", + " [7],\n", + " [8]]), Tensor(shape=[3, 1], dtype=Int64, value=\n", + "[[0],\n", + " [2],\n", + " [0]]))\n", + "mindspore output:\n", + " [[9 3 4 5]\n", + " [5 2 7 4]\n", + " [8 1 3 6]]\n" + ] + }, + { + "ename": "TypeError", + "evalue": "Failed calling max with \"max(Tensor, axis=int, keepdims=bool, initial=int, where=Tensor, return_indices=bool)\".\nThe valid calling should be:\n\"max(input=)\"\n\"max(input=, dim=, keepdim=)\"\n\"max(input=, other=)\"\n\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/op_function/converter.h:112 Parse\n", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[47], line 11\u001b[0m\n\u001b[1;32m 9\u001b[0m y1_output \u001b[38;5;241m=\u001b[39m ms\u001b[38;5;241m.\u001b[39mmint\u001b[38;5;241m.\u001b[39mmax(y1, other\u001b[38;5;241m=\u001b[39mother)\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmindspore output:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m,y1_output)\n\u001b[0;32m---> 11\u001b[0m y1_output \u001b[38;5;241m=\u001b[39m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmint\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmax\u001b[49m\u001b[43m(\u001b[49m\u001b[43my1\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdims\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43minitial\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mwhere\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mtensor\u001b[49m\u001b[43m(\u001b[49m\u001b[43mwhere\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mreturn_indices\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n\u001b[1;32m 12\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmindspore output:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m,y1_output)\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/ops/functional_overload.py:511\u001b[0m, in \u001b[0;36mmax\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m 443\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmax\u001b[39m(\u001b[38;5;241m*\u001b[39margs, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs):\n\u001b[1;32m 444\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124mr\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 445\u001b[0m \u001b[38;5;124;03m max(input) -> Tensor\u001b[39;00m\n\u001b[1;32m 446\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 509\u001b[0m \u001b[38;5;124;03m For details, please refer to :func:`mindspore.mint.maximum`.\u001b[39;00m\n\u001b[1;32m 510\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 511\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_max_instance\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[0;31mTypeError\u001b[0m: Failed calling max with \"max(Tensor, axis=int, keepdims=bool, initial=int, where=Tensor, return_indices=bool)\".\nThe valid calling should be:\n\"max(input=)\"\n\"max(input=, dim=, keepdim=)\"\n\"max(input=, other=)\"\n\n\n----------------------------------------------------\n- C++ Call Stack: (For framework developers)\n----------------------------------------------------\nmindspore/ccsrc/pipeline/pynative/op_function/converter.h:112 Parse\n" + ] + } + ], + "source": [ + "y1_output = ms.mint.max(y1)\n", + "print ('mindspore output:\\n',y1_output)\n", + "y1_output = ms.mint.max(y1, 1, True)\n", + "print ('mindspore output:\\n',y1_output)\n", + "\n", + "other = ms.tensor([[0, 0, 0, 0],\n", + " [0, 1, 0, 1],\n", + " [1, 1, 1, 0]])\n", + "y1_output = ms.mint.max(y1, other=other)\n", + "print ('mindspore output:\\n',y1_output)" + ] + }, { "cell_type": "markdown", "id": "6d461b44-93c6-43a8-a6ca-160b0b82c743", @@ -124,7 +223,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 48, "id": "9f3d8f1b-4539-4b5b-83c2-4221a401a60d", "metadata": {}, "outputs": [ @@ -133,13 +232,7 @@ "output_type": "stream", "text": [ "mindspore output:\n", - " (Tensor(shape=[3, 1], dtype=Int64, value=\n", - "[[9],\n", - " [7],\n", - " [8]]), Tensor(shape=[3, 1], dtype=Int64, value=\n", - "[[0],\n", - " [2],\n", - " [0]]))\n", + " (Tensor(shape=[3], dtype=Int64, value= [9, 7, 8]), Tensor(shape=[3], dtype=Int64, value= [0, 2, 0]))\n", "\n", "\n", "torch output:\n", @@ -240,28 +333,67 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 29, "id": "1f0ea595-beca-48ea-8154-adfb94bca0a8", "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'AscendOpPrecisionConf' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[29], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mms\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdevice_context\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mascend\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mop_precision\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmatmul_allow_hf32\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n", + "File \u001b[0;32m/opt/anaconda3/envs/mindspore/lib/python3.9/site-packages/mindspore/device_context/ascend/op_precision.py:138\u001b[0m, in \u001b[0;36mmatmul_allow_hf32\u001b[0;34m(value)\u001b[0m\n\u001b[1;32m 135\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mFor \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mmatmul_allow_hf32\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m, the type of input value must be one of \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 136\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00msupported_modes\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m, but got \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mvalue\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m.\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 137\u001b[0m is_enable \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m1\u001b[39m\u001b[38;5;124m\"\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m value \u001b[38;5;28;01melse\u001b[39;00m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m0\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m--> 138\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_enable \u001b[38;5;241m==\u001b[39m \u001b[43mAscendOpPrecisionConf\u001b[49m\u001b[38;5;241m.\u001b[39mget_instance()\u001b[38;5;241m.\u001b[39mmatmul_allow_hf32():\n\u001b[1;32m 139\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m\n\u001b[1;32m 140\u001b[0m \u001b[38;5;66;03m# Check the configuration environment whether valid\u001b[39;00m\n", + "\u001b[0;31mNameError\u001b[0m: name 'AscendOpPrecisionConf' is not defined" + ] + } + ], + "source": [ + "ms.device_context.ascend.op_precision.matmul_allow_hf32(True)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "5cfb0e5a-8c17-4897-b55e-06c63c03b209", + "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "torch output:\n", - " tensor([[10, 3, 4, 5],\n", - " [ 5, 2, 7, 4],\n", - " [ 8, 1, 5, 6]])\n" + " torch.return_types.max(\n", + "values=tensor([[ 9, 7, 1],\n", + " [10, 7, 8]]),\n", + "indices=tensor([[0, 2, 0],\n", + " [0, 2, 0]]))\n" + ] + }, + { + "ename": "TypeError", + "evalue": "maximum(): argument 'other' (position 1) must be Tensor, not int", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[25], line 11\u001b[0m\n\u001b[1;32m 9\u001b[0m y2_output \u001b[38;5;241m=\u001b[39m y2\u001b[38;5;241m.\u001b[39mmax(\u001b[38;5;241m2\u001b[39m)\n\u001b[1;32m 10\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtorch output:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m,y2_output)\n\u001b[0;32m---> 11\u001b[0m y2_output_2 \u001b[38;5;241m=\u001b[39m \u001b[43my2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmaximum\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 12\u001b[0m \u001b[38;5;28mprint\u001b[39m (\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtorch output:\u001b[39m\u001b[38;5;130;01m\\n\u001b[39;00m\u001b[38;5;124m'\u001b[39m,y2_output_2)\n", + "\u001b[0;31mTypeError\u001b[0m: maximum(): argument 'other' (position 1) must be Tensor, not int" ] } ], "source": [ - "other=np.array([[10, 0, 0, 0],\n", - " [0, 1, 0, 1],\n", - " [1, 1, 5, 0]])\n", - "y2 = torch.tensor(input)\n", - "y2_output = y2.max(torch.tensor(other))\n", - "print ('torch output:\\n',y2_output)" + "input = np.array([[[9, 3, 4, 5],\n", + " [5, 2, 7, 4],\n", + " [1, 1, 1, 1]],\n", + " [[10, 3, 4, 5],\n", + " [ 5, 2, 7, 4],\n", + " [ 8, 1, 5, 6]]])\n", + "\n", + "y2 = ms.tensor(input)\n", + "y2_output = ms.ops.max(2)" ] }, {