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

add index_select op #5661

Merged
merged 29 commits into from
Aug 12, 2021
Merged

add index_select op #5661

merged 29 commits into from
Aug 12, 2021

Conversation

Ikkyu321
Copy link
Contributor

@Ikkyu321 Ikkyu321 commented Jul 29, 2021

图片
图片

@CLAassistant
Copy link

CLAassistant commented Jul 29, 2021

CLA assistant check
All committers have signed the CLA.

@Ikkyu321 Ikkyu321 requested a review from hengzi August 3, 2021 07:23
@hengzi
Copy link
Contributor

hengzi commented Aug 5, 2021

添加文档截图 参考 #5636

oneflow/docs 执行 make html 生成html文件,下载到电脑用浏览器打开



@register_tensor_op("index_select")
def index_select_op(input, dim, index, sparse_grad=False):
Copy link
Contributor

Choose a reason for hiding this comment

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

flow.xxxflow.Tensor.xxx 共用一个函数的话,就共用了 docstring,那么总有一方的文档是不准确的。
需要为 flow.Tensor.xxx 单独准备一个函数,书写 docstring。
可以参考 acos 等算子的写法

Copy link
Contributor Author

Choose a reason for hiding this comment

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

flow.xxxflow.Tensor.xxx 共用一个函数的话,就共用了 docstring,那么总有一方的文档是不准确的。
需要为 flow.Tensor.xxx 单独准备一个函数,书写 docstring。
可以参考 acos 等算子的写法

已修改

Copy link
Contributor

Choose a reason for hiding this comment

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

按照 torch 的 API 文档 https://pytorch.org/docs/stable/generated/torch.index_select.html?highlight=index_select#torch.index_select

这个算子的原型是:

torch.index_select(input, dim, index, *, out=None) → Tensor

考虑到暂时不对齐 out 参数,那么原型也应该是

.index_select(input, dim, index) 

这里的 sparse_grad 是那个版本的呢?

Copy link
Contributor

Choose a reason for hiding this comment

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

PR 描述里的截图是不对的,应该更行
image

flow.Tensor.index_select 的截图也应该补上

input.index_select(dim, index, sparse_grad) -> Tensor
See :func:`oneflow.index_select`
"""
assert sparse_grad is False, "Only support bool = False for now!"
Copy link
Contributor

Choose a reason for hiding this comment

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

不必要把一样的代码写两遍,直接调用 index_select_op 就可以了

index_select_op(input, dim, index, sparse_grad)



@register_tensor_op("index_select")
def index_select_op(input, dim, index, sparse_grad=False):
Copy link
Contributor

Choose a reason for hiding this comment

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

按照 torch 的 API 文档 https://pytorch.org/docs/stable/generated/torch.index_select.html?highlight=index_select#torch.index_select

这个算子的原型是:

torch.index_select(input, dim, index, *, out=None) → Tensor

考虑到暂时不对齐 out 参数,那么原型也应该是

.index_select(input, dim, index) 

这里的 sparse_grad 是那个版本的呢?



@register_tensor_op("index_select")
def index_select_op(input, dim, index, sparse_grad=False):
Copy link
Contributor

Choose a reason for hiding this comment

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

PR 描述里的截图是不对的,应该更行
image

flow.Tensor.index_select 的截图也应该补上

Args:
input (Tensor): the source tensor
dim (int): the axis along which to index
index (LongTensor): the indices of elements to select
Copy link
Contributor

Choose a reason for hiding this comment

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

flow 里面还没有 LongTensor

Copy link
Contributor

Choose a reason for hiding this comment

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

     the 1-D tensor containing the indices to index

我给的建议是直接参考 torch 的

Copy link
Contributor Author

Choose a reason for hiding this comment

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

     the 1-D tensor containing the indices to index

我给的建议是直接参考 torch 的

已修改



def index_select_op(input, dim, index, sparse_grad=False):
r"""Select values along an axis specified by `dim`.
Copy link
Contributor

Choose a reason for hiding this comment

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

加一个对 PyTorch 参考的说明。可以参考 Conv1d

Copy link
Contributor Author

Choose a reason for hiding this comment

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

加一个对 PyTorch 参考的说明。可以参考 Conv1d

已修改

"""
assert sparse_grad is False, "Only support bool = False for now!"
assert len(index.shape) == 1, "Dimensions of index should be an LongTensor"
assert dim < len(input.shape) and dim > -1, "Value of dim is out of range"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert dim < len(input.shape) and dim > -1, "Value of dim is out of range"
assert dim < len(input.shape) and dim >= 0, "Value of dim is out of range"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

assert sparse_grad is False, "Only support bool = False for now!"
assert len(index.shape) == 1, "Dimensions of index should be an LongTensor"
assert dim < len(input.shape) and dim > -1, "Value of dim is out of range"
assert _input_args_is_int(index.tolist()), "input index parameter is not illegal!"
Copy link
Contributor

Choose a reason for hiding this comment

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

我觉得这里的提示可以更明确点,告诉用户怎样做可以修正好。而不是只告诉用户他做的不合法

Copy link
Contributor Author

Choose a reason for hiding this comment

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

我觉得这里的提示可以更明确点,告诉用户怎样做可以修正好。而不是只告诉用户他做的不合法

已修改

@register_tensor_op("index_select")
def index_select_op_tensor(input, dim, index, sparse_grad=False):
"""
input.index_select(dim, index, sparse_grad) -> Tensor
Copy link
Contributor

@doombeaker doombeaker Aug 6, 2021

Choose a reason for hiding this comment

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

函数原型是错的(不应该有 sparase_grad)

可以直接参考 https://pytorch.org/docs/1.9.0/generated/torch.Tensor.index_select.html?highlight=index_select#torch.Tensor.index_select 或者 oneflow 已有的其它例子

Copy link
Contributor Author

Choose a reason for hiding this comment

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

函数原型是错的(不应该有 sparase_grad)

可以直接参考 https://pytorch.org/docs/1.9.0/generated/torch.Tensor.index_select.html?highlight=index_select#torch.Tensor.index_select 或者 oneflow 已有的其它例子

已修改

device = random_device()

# test 4 dimensions tensor
axis = random(0, 4).to(int)
Copy link
Contributor

Choose a reason for hiding this comment

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

我不是很赞同 axis 和 dim 混用。 虽然无伤大雅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

我不是很赞同 axis 和 dim 混用。 虽然无伤大雅

修改为dim

x = index_i.expand(index_rshp)
index_gather = flow.cat((index_gather, x), dim)

return flow.gather(input, index_gather, dim, sparse_grad)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return flow.gather(input, index_gather, dim, sparse_grad)
return flow.gather(input, index_gather, dim)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

已修改

tensor_dim.append(random(2, 6).to(int).value())

index = random_pytorch_tensor(
ndim=1, low=0, high=tensor_dim[dim.value()], dtype=int
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ndim=1, low=0, high=tensor_dim[dim.value()], dtype=int
ndim=1, dim0=dim.value()+1, low=0, high=tensor_dim[dim.value()], dtype=int

现在这样的测试案例 ,index 是shape 总是 [1],不能充分覆盖各种情况。

Copy link
Contributor Author

Choose a reason for hiding this comment

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

现在这样的测试案例 ,index 是shape 总是 [1],不能充分覆盖各种情况。

已修改index的长度为随机(1,10)

@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 12, 2021 08:36
@oneflow-ci-bot oneflow-ci-bot self-requested a review August 12, 2021 10:30
@oneflow-ci-bot oneflow-ci-bot removed their request for review August 12, 2021 12:24
@oneflow-ci-bot oneflow-ci-bot self-requested a review August 12, 2021 13:51
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 12, 2021 15:39
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 12, 2021 17:47
@oneflow-ci-bot oneflow-ci-bot requested review from oneflow-ci-bot and removed request for oneflow-ci-bot August 12, 2021 19:53
@github-actions
Copy link
Contributor

Speed stats:
GPU Name: GeForce GTX 1080 

PyTorch resnet50 time: 140.8ms (= 7039.8ms / 50, input_shape=[16, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 128.1ms (= 6402.7ms / 50, input_shape=[16, 3, 224, 224], backward is enabled)
Relative speed: 1.10 (= 140.8ms / 128.1ms)

PyTorch resnet50 time: 87.1ms (= 4353.6ms / 50, input_shape=[8, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 74.5ms (= 3724.0ms / 50, input_shape=[8, 3, 224, 224], backward is enabled)
Relative speed: 1.17 (= 87.1ms / 74.5ms)

PyTorch resnet50 time: 59.9ms (= 2996.0ms / 50, input_shape=[4, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 48.5ms (= 2423.7ms / 50, input_shape=[4, 3, 224, 224], backward is enabled)
Relative speed: 1.24 (= 59.9ms / 48.5ms)

PyTorch resnet50 time: 48.7ms (= 2435.6ms / 50, input_shape=[2, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 38.1ms (= 1903.1ms / 50, input_shape=[2, 3, 224, 224], backward is enabled)
Relative speed: 1.28 (= 48.7ms / 38.1ms)

PyTorch resnet50 time: 40.1ms (= 2004.1ms / 50, input_shape=[1, 3, 224, 224], backward is enabled)
OneFlow resnet50 time: 36.2ms (= 1810.5ms / 50, input_shape=[1, 3, 224, 224], backward is enabled)
Relative speed: 1.11 (= 40.1ms / 36.2ms)

@oneflow-ci-bot oneflow-ci-bot merged commit bcd3116 into master Aug 12, 2021
@oneflow-ci-bot oneflow-ci-bot deleted the dev_index_select branch August 12, 2021 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants