agrabows commented on code in PR #21115:
URL: https://github.com/apache/incubator-mxnet/pull/21115#discussion_r946714998
##########
tests/python/dnnl/subgraphs/test_matmul_subgraph.py:
##########
@@ -27,25 +27,47 @@
class MultiHeadAttention(nn.HybridBlock):
- def __init__(self, units, num_heads, dtype='float32', negative_case=False,
**kwargs):
+ def __init__(self, units, num_heads, batch_size=-1, seq_length=-1,
dtype='float32', negative_case=False, no_split_case = False, **kwargs):
super(MultiHeadAttention, self).__init__(**kwargs)
self._units = units
self._num_heads = num_heads
self._fc = nn.Dense(in_units=self._units, units=3*self._units,
flatten=False, dtype=dtype)
self._scale = math.sqrt(self._units // self._num_heads)
self.negative_case = negative_case
+ self.no_split_case = no_split_case
+ self.batch_size = batch_size
+ self.seq_length = seq_length
def forward(self, x, mask):
out = self._fc(x)
query, key, value = mx.np.split(out, 3, axis=-1)
+ if self.no_split_case:
+ key = mx.np.expand_dims(key, 3)
+ key = mx.np.broadcast_to(key, (key.shape[0], key.shape[1],
key.shape[2], self._num_heads))
+ key = mx.np.reshape(key, (key.shape[0], key.shape[1]*self._num_heads,
key.shape[2]))
+ value = mx.np.expand_dims(value, 3)
+ value = mx.np.broadcast_to(value, (value.shape[0], value.shape[1],
value.shape[2], self._num_heads))
+ value = mx.np.reshape(value, (value.shape[0],
value.shape[1]*self._num_heads, value.shape[2]))
+ query = mx.np.reshape(query, (-2, -2, self._num_heads, -1))
if self.negative_case:
- key = key * 2
- query = mx.npx.reshape(query, (-2, -2, self._num_heads, -1))
- key = mx.npx.reshape(key, (-2, -2, self._num_heads, -1))
- value = mx.npx.reshape(value, (-2, -2, self._num_heads, -1))
+ shape_from = self.batch_size * self.seq_length * self._units
+ shape_to = self.batch_size * self._num_heads * self.seq_length*
self.seq_length
Review Comment:
this part was deleted
##########
tests/python/dnnl/subgraphs/test_matmul_subgraph.py:
##########
@@ -27,25 +27,47 @@
class MultiHeadAttention(nn.HybridBlock):
- def __init__(self, units, num_heads, dtype='float32', negative_case=False,
**kwargs):
+ def __init__(self, units, num_heads, batch_size=-1, seq_length=-1,
dtype='float32', negative_case=False, no_split_case = False, **kwargs):
super(MultiHeadAttention, self).__init__(**kwargs)
self._units = units
self._num_heads = num_heads
self._fc = nn.Dense(in_units=self._units, units=3*self._units,
flatten=False, dtype=dtype)
self._scale = math.sqrt(self._units // self._num_heads)
self.negative_case = negative_case
+ self.no_split_case = no_split_case
+ self.batch_size = batch_size
+ self.seq_length = seq_length
def forward(self, x, mask):
out = self._fc(x)
query, key, value = mx.np.split(out, 3, axis=-1)
+ if self.no_split_case:
+ key = mx.np.expand_dims(key, 3)
+ key = mx.np.broadcast_to(key, (key.shape[0], key.shape[1],
key.shape[2], self._num_heads))
+ key = mx.np.reshape(key, (key.shape[0], key.shape[1]*self._num_heads,
key.shape[2]))
+ value = mx.np.expand_dims(value, 3)
+ value = mx.np.broadcast_to(value, (value.shape[0], value.shape[1],
value.shape[2], self._num_heads))
+ value = mx.np.reshape(value, (value.shape[0],
value.shape[1]*self._num_heads, value.shape[2]))
+ query = mx.np.reshape(query, (-2, -2, self._num_heads, -1))
if self.negative_case:
- key = key * 2
- query = mx.npx.reshape(query, (-2, -2, self._num_heads, -1))
- key = mx.npx.reshape(key, (-2, -2, self._num_heads, -1))
- value = mx.npx.reshape(value, (-2, -2, self._num_heads, -1))
+ shape_from = self.batch_size * self.seq_length * self._units
+ shape_to = self.batch_size * self._num_heads * self.seq_length*
self.seq_length
+ if shape_to >= shape_from:
+ shape_dif = int(shape_to/shape_from) + 1
+ else:
+ shape_dif = int(shape_from/shape_to) + 1
+ negative_test_tensor = mx.np.reshape(query, (shape_from, 1))
+ negative_test_tensor = mx.np.broadcast_to(negative_test_tensor,
(shape_from, shape_dif))
+ negative_test_tensor = negative_test_tensor.flatten()
+ negative_test_tensor = mx.np.split(negative_test_tensor, [shape_to])[0]
+ negative_test_tensor = mx.np.reshape(negative_test_tensor,
(self.batch_size,self._num_heads,self.seq_length,self.seq_length))
Review Comment:
this part was deleted
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]