Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 17 additions & 1 deletion python/tvm/relay/frontend/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -1863,6 +1863,13 @@ def chunk(self, inputs, input_types):

return _op.split(data, indeces, axis)

def baddbmm(self, inputs, _):
input = inputs[0]
batch1, batch2 = inputs[1:3]
beta = _expr.const(float(inputs[3]))
alpha = _expr.const(float(inputs[4]))
return beta * input + alpha * _op.nn.batch_matmul(batch1, batch2, transpose_b=False)

def matmul(self, inputs, input_types):

inputs_0 = inputs[0]
Expand Down Expand Up @@ -2565,7 +2572,14 @@ def numel(self, inputs, input_types):
return _op.ndarray_size(inputs[0])

def empty(self, inputs, input_types):
shape = inputs[0]
shape = []
for s in inputs[0]:
if isinstance(s, _expr.Constant):
shape.append(s.data.numpy().item())
else:
assert isinstance(s, int)
shape.append(s)

return _op.zeros(shape, _convert_dtype_value(inputs[1]))

def empty_like(self, inputs, input_types):
Expand Down Expand Up @@ -3621,6 +3635,7 @@ def create_convert_map(self):
"aten::unsafe_chunk": self.chunk,
"aten::matmul": self.matmul,
"aten::bmm": self.matmul,
"aten::baddbmm": self.baddbmm,
"aten::expand": self.expand,
"aten::Int": self.int,
"prim::NumToTensor": self.numtotensor,
Expand Down Expand Up @@ -4587,6 +4602,7 @@ def from_pytorch(
if inp.type().kind() == "TupleType" or inp.type().kind() == "ListType":
enable_lower_all_tuples = False
break

_run_jit_passes(graph, enable_lower_all_tuples)

if custom_convert_map:
Expand Down
16 changes: 15 additions & 1 deletion tests/python/frontend/pytorch/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
# pylint: disable=import-self, invalid-name, unused-argument
# pylint: disable=import-self, invalid-name, unused-argument, missing-function-docstring
"""Unit tests for various models and operators"""
import os
import platform
Expand Down Expand Up @@ -5038,5 +5038,19 @@ def _test_multinomial(num_samples):
)


@tvm.testing.uses_gpu
def test_baddbmm():
def test_fn(alpha, beta):
return lambda inp, batch1, batch2: torch.baddbmm(
inp, batch1, batch2, beta=beta, alpha=alpha
)

M = torch.randn(10, 3, 5)
batch1 = torch.randn(10, 3, 4)
batch2 = torch.randn(10, 4, 5)

verify_model(test_fn(0.5, 1.0), [M, batch1, batch2])


if __name__ == "__main__":
tvm.testing.main()