-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[TOPI] FIFO buffer op, to accelerate sequence modeling with dilated convolutions #4039
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
48afe60
Add FIFO buffer op to enable explicit computation re-use in convolution
hcho3 b200715
Add a test
hcho3 f8d7ae7
Add end-to-end test with 1D convolution
hcho3 1f2c839
Add a stub in MXNet frontend
hcho3 915f2ce
Address reviewer comments
hcho3 8374d6e
Add back stub for MXNet frontend
hcho3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,3 +22,4 @@ | |
| from .batch_matmul import * | ||
| from .sparse import * | ||
| from .pad import * | ||
| from .fifo_buffer import * | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|
|
||
| """FIFO buffer op""" | ||
| from __future__ import absolute_import as _abs | ||
| import tvm | ||
| from .. import tag | ||
| from ..transform import concatenate, strided_slice | ||
|
|
||
| @tvm.tag_scope(tag=tag.INJECTIVE+",fifo_buffer") | ||
| def fifo_buffer(data, buffer, axis): | ||
hcho3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Implements the FIFO buffer | ||
| """ | ||
| assert len(data.shape) == len(buffer.shape), \ | ||
| 'buffer and data must have same number of dimensions, ' + \ | ||
| 'buffer.shape = {}, data.shape = {}'.format(buffer.shape, data.shape) | ||
| assert len(buffer.shape) >= 1, 'Zero-dimension tensor not supported' | ||
| assert 0 <= axis < len(buffer.shape), 'buffer axis out of range' | ||
| for i in range(len(data.shape)): | ||
| if i == axis: | ||
| assert int(str(data.shape[i])) <= int(str(buffer.shape[i])) | ||
| else: | ||
| assert int(str(data.shape[i])) == int(str(buffer.shape[i])) | ||
|
|
||
| buflen = buffer.shape[axis] | ||
| data_size = data.shape[axis] | ||
|
|
||
| # Explicitly write out formula up to 4D, and then use concat+slice combo for 5D and higher | ||
| if len(buffer.shape) == 1: | ||
| return tvm.compute(buffer.shape, | ||
| lambda i: | ||
| tvm.if_then_else(i < buflen - data_size, | ||
| buffer[i + data_size], | ||
| data[i - buflen + data_size]), | ||
| name='new_buffer') | ||
| elif len(buffer.shape) == 2: | ||
| if axis == 0: | ||
| return tvm.compute(buffer.shape, | ||
| lambda i, j: | ||
| tvm.if_then_else(i < buflen - data_size, | ||
| buffer[i + data_size, j], | ||
| data[i - buflen + data_size, j]), | ||
| name='new_buffer') | ||
| if axis == 1: | ||
| return tvm.compute(buffer.shape, | ||
| lambda i, j: | ||
| tvm.if_then_else(j < buflen - data_size, | ||
| buffer[i, j + data_size], | ||
| data[i, j - buflen + data_size]), | ||
| name='new_buffer') | ||
| assert False, 'Invalid value for axis; it should be at most {}'.format(len(buffer.shape)) | ||
| elif len(buffer.shape) == 3: | ||
| if axis == 0: | ||
| return tvm.compute(buffer.shape, | ||
| lambda i, j, k: | ||
| tvm.if_then_else(i < buflen - data_size, | ||
| buffer[i + data_size, j, k], | ||
| data[i - buflen + data_size, j, k]), | ||
| name='new_buffer') | ||
| if axis == 1: | ||
| return tvm.compute(buffer.shape, | ||
| lambda i, j, k: | ||
| tvm.if_then_else(j < buflen - data_size, | ||
| buffer[i, j + data_size, k], | ||
| data[i, j - buflen + data_size, k]), | ||
| name='new_buffer') | ||
| if axis == 2: | ||
| return tvm.compute(buffer.shape, | ||
| lambda i, j, k: | ||
| tvm.if_then_else(k < buflen - data_size, | ||
| buffer[i, j, k + data_size], | ||
| data[i, j, k - buflen + data_size]), | ||
| name='new_buffer') | ||
| assert False, 'Invalid value for axis; it should be at most {}'.format(len(buffer.shape)) | ||
| elif len(buffer.shape) == 4: | ||
| if axis == 0: | ||
| return tvm.compute(buffer.shape, | ||
| lambda i, j, k, l: | ||
| tvm.if_then_else(i < buflen - data_size, | ||
| buffer[i + data_size, j, k, l], | ||
| data[i - buflen + data_size, j, k, l]), | ||
| name='new_buffer') | ||
| if axis == 1: | ||
| return tvm.compute(buffer.shape, | ||
| lambda i, j, k, l: | ||
| tvm.if_then_else(j < buflen - data_size, | ||
| buffer[i, j + data_size, k, l], | ||
| data[i, j - buflen + data_size, k, l]), | ||
| name='new_buffer') | ||
| if axis == 2: | ||
| return tvm.compute(buffer.shape, | ||
| lambda i, j, k, l: | ||
| tvm.if_then_else(k < buflen - data_size, | ||
| buffer[i, j, k + data_size, l], | ||
| data[i, j, k - buflen + data_size, l]), | ||
| name='new_buffer') | ||
| if axis == 3: | ||
| return tvm.compute(buffer.shape, | ||
| lambda i, j, k, l: | ||
| tvm.if_then_else(l < buflen - data_size, | ||
| buffer[i, j, k, l + data_size], | ||
| data[i, j, k, l - buflen + data_size]), | ||
| name='new_buffer') | ||
| assert False, 'Invalid value for axis; it should be at most {}'.format(len(buffer.shape)) | ||
| else: | ||
| # Implement FIFO buffer as combination of concat and slice | ||
| begin = [0] * len(buffer.shape) | ||
| begin[axis] = data.shape[axis] | ||
| end = list(buffer.shape[:]) | ||
| end[axis] += data.shape[axis] | ||
| return strided_slice(concatenate((buffer, data), axis=axis), begin=begin, end=end) | ||
| return None | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.