Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Hackathon No.27】为 Paddle 新增 frac 数学计算API #51

Merged
merged 3 commits into from
Mar 24, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions rfcs/APIs/20220319_api_design_for_frac.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# paddle.frac设计文档

| API名称 | 新增API名称 |
| ------------------------------------------------------------ | ------------------------------- |
| 提交作者<input type="checkbox" class="rowselector hidden"> | Asthestarsfalll |
| 提交时间<input type="checkbox" class="rowselector hidden"> | 2022-03-19 |
| 版本号 | V1.0 |
| 依赖飞桨版本<input type="checkbox" class="rowselector hidden"> | develop |
| 文件名 | 20200319_api_design_for_frac.md |


# 一、概述

## 1、相关背景

frac用于计算输入中每个元素的分数部分。

## 2、功能目标

为 Paddle 新增 frac 数学计算API。

## 3、意义

Paddle支持frac数学计算API。

# 二、飞桨现状

飞浆目前并无此API,具体实现可由`trunc`和`elementwise_sub`组合实现。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 笔误:飞桨
  • 具体实现可由truncelementwise_sub组合实现。是在python端调这两个的C++ OP呢,还是直接用这两个python组合呢?如果后者的话,接口是paddle.subtract,也可以直接用-

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

调用了C++OP,elementwise_sub调用的是_elementwise_op_elementwise_op_in_dygraph

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,那可以明确说一下是在python端调用C++ Op实现。



# 三、业内方案调研

## Pytorch

Pytorch中有API `torch.frac()`和`Tensor.frac()`,介绍为:

```
Computes the fractional portion of each element in input.
```

### 实现方法

Pytorch中实际上使用输入tensor减去trunc后的结果得到输出,一些相关代码如下:

[代码位置](https://github.com/pytorch/pytorch/blob/2d110d514f9611dd00bf63ae5ef7d5ce017c900f/torch/csrc/jit/codegen/cuda/runtime/helpers.cu)

```c++
__device__ double frac(double x) {
return x - trunc(x);
}

__device__ float frac(float x) {
return x - trunc(x);
}
```

[代码位置](https://github.com/pytorch/pytorch/blob/3a0c680a14d2f1211adc4dfcc7ab0be5d1f1f214/torch/csrc/jit/codegen/fuser/cpu/resource_strings.h#L46)

```c++
double frac(double x) {
return x - trunc(x);
}
float fracf(float x) {
return x - truncf(x);
}
```

[代码位置](https://github.com/pytorch/pytorch/blob/f64906f470916c3edc1937155a8a37e77c35f393/aten/src/ATen/test/vec_test_all_types.h)

```c++
template <typename T>
T frac(T x) {
return x - std::trunc(x);
}
```

## TensorFlow

未找到相关实现,在[该文件](https://github.com/tensorflow/tensorflow/blob/a0192a3285e2d010ae57a76cc8e8981632655cb9/tensorflow/compiler/mlir/tensorflow/transforms/legalize_hlo_patterns.td#L256)注释中的有所提及,如下:

```
frac = x - floor(x)
```



# 四、对比分析

Pytorch与TensorFlow思路一致。

# 五、设计思路与实现方案

## 命名与参数设计

API设计为`paddle.frac(x, name=None)`和tensor.frac(x, name=None)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tensor.frac(x, name=None)


## 底层OP设计

使用先用API组合完成,无需设计底层OP

## API实现方案

使用`trunc`与`elementwise_sub`组合实现,实现位置为`paddle/tensor/math.py`与`sum`,`nansum`等方法放在一起:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

应该和trunc放在一起


1. 首先使用`trunc`获取输入tensor的整数部分;
2. 再用输入x减去上一步得到的整数部分即可获取小数部分。

# 六、测试和验收的考量

测试考虑的case如下:

- 在动态图、静态图下,与numpy结果的一致性。

# 七、可行性分析和排期规划

方案主要依赖现有paddle api组合而成,工期上可以满足在当前版本周期内开发完成。

# 八、影响面

为独立新增API,对其他模块没有影响

# 名词解释


# 附件及参考资料