forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TOPI] FIFO buffer op, to accelerate sequence modeling with dilated c…
…onvolutions (apache#4039) * Add FIFO buffer op to enable explicit computation re-use in convolution * Add a test * Add end-to-end test with 1D convolution * Add a stub in MXNet frontend * Address reviewer comments * Add back stub for MXNet frontend
- Loading branch information
Showing
9 changed files
with
462 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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): | ||
""" | ||
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.