免费网站建设可信吗,wordpress 软件站主题,装修公司网页设计,在手机上怎么建造网站笔者最近在尝试实现AlexNet的底层算子#xff0c;基于pytorch的框架#xff0c;本文主要记录一下pytorch中是如何实现relu算子的。 首先最外层是位于torch\nn\modules\activation.py#xff0c;主要代码如下#xff1a; __constants__ [inplace]inplace: bool…笔者最近在尝试实现AlexNet的底层算子基于pytorch的框架本文主要记录一下pytorch中是如何实现relu算子的。 首先最外层是位于torch\nn\modules\activation.py主要代码如下 __constants__ [inplace]inplace: booldef __init__(self, inplace: bool False):super().__init__()self.inplace inplacedef forward(self, input: Tensor) - Tensor:return F.relu(input, inplaceself.inplace)def extra_repr(self) - str:inplace_str inplaceTrue if self.inplace else return inplace_str调用的是位于torch\nn\functional.py的如下代码
def relu(input: Tensor, inplace: bool False) - Tensor: # noqa: D400,D402rrelu(input, inplaceFalse) - TensorApplies the rectified linear unit function element-wise. See:class:~torch.nn.ReLU for more details.if has_torch_function_unary(input):return handle_torch_function(relu, (input,), input, inplaceinplace)if inplace:result torch.relu_(input)else:result torch.relu(input)return result然后调用的是aten\src\ATen\native\Activation.cpp的如下代码
Tensor relu(const Tensor self) {TORCH_CHECK(self.scalar_type() ! at::kBool, Boolean inputs not supported for relu);return at::clamp_min(self, 0);
}可以看到主要就是一个大小的比较。
pytorch调试工具
先说问题只能看到python的处理逻辑不能看到底层的C的处理逻辑。 如何使用参考的是这篇文章。注意pdb虽然是python内置的包但是仍然需要通过import pdb导入才能使用。
还有一个问题就是pytorch是如何通过python代码调用C代码的留到下一篇博文更新。