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
PyTorch to CoreML model conversion with coremltools v4.0b1 for a TransformerEncoder model/module does not complete. Missing op: bmm. #768
Comments
@leovinus2001 Yes, you are right, bmm stands for batched matrix multiplication. Core ML supports this layer. We will hook it up in the torch frontend as soon as possible. Thanks for reporting the issue and such a detailed bug reporting. Appreciate your effort and help. |
You are welcome. Appreciate the fixes! |
Just politely wondering whether there is any ETA on this fix? Happy to verify it. |
@leovinus2001 Can you please add the following snippet in the conversion code to check if it fixes the issue? Thanks.
|
@DawerG Thanks, that works like a charm for me :) Conversion now runs successfully and the inference on float32 and int8 quantized models is good as well. One question, for |
@leovinus2001 Thanks for verifying :) Appreciate your help. Yes, right. That would be better. We should change the snippet to We will ensure that this change is covered as part of the next beta release. |
Nice, coremltools 4.0b3 with PyTorch 1.6.0 fixes the missing "bmm" op. |
Closing |
@leovinus2001 Hi, did you got the same speed of float32 and int8 model? |
same ops worked for mm if anyone is interested |
Relevance:
Transformers train faster and deliver often better models.
The coremltools v4.0 converter from PyTorch to coreml format .mlmodel should handle it well for embedded work.
Background:
RuntimeError: PyTorch convert function for op bmm not implemented
diff --git a/coremltools/converters/mil/frontend/torch/ops.py b/coremltools/converters/mil/frontend/torch/ops.py
index 1e19c7c..be2b13d 100644
--- a/coremltools/converters/mil/frontend/torch/ops.py
+++ b/coremltools/converters/mil/frontend/torch/ops.py
@@ -47,6 +47,7 @@ def convert_nodes(context, graph):
for node in _tqdm(graph.nodes, desc="Converting Frontend ==> MIL Ops", unit=" ops"):
_add_op = _TORCH_OPS_REGISTRY.get(node.kind, None)
_logging.info("Converting op {} : {}".format(node.name, node.kind))
@@ -955,7 +956,8 @@ def lstm(context, node):
)
Reproducible:
Yes
Testcase:
Attached
testTransformerEncoder.txt
Setup:
macOS Catalina latest
Python 3.7.6
Log:
Torch version : 1.5.1
CoreML tools version : 4.0b1
TestModel(
(transformer_encoder): TransformerEncoder(
(layers): ModuleList(
(0): TransformerEncoderLayer(
(self_attn): MultiheadAttention(
(out_proj): Linear(in_features=28, out_features=28, bias=True)
)
(linear1): Linear(in_features=28, out_features=16, bias=True)
(dropout): Dropout(p=0.1, inplace=False)
(linear2): Linear(in_features=16, out_features=28, bias=True)
(norm1): LayerNorm((28,), eps=1e-05, elementwise_affine=True)
(norm2): LayerNorm((28,), eps=1e-05, elementwise_affine=True)
(dropout1): Dropout(p=0.1, inplace=False)
(dropout2): Dropout(p=0.1, inplace=False)
)
(1): TransformerEncoderLayer(
(self_attn): MultiheadAttention(
(out_proj): Linear(in_features=28, out_features=28, bias=True)
)
(linear1): Linear(in_features=28, out_features=16, bias=True)
(dropout): Dropout(p=0.1, inplace=False)
(linear2): Linear(in_features=16, out_features=28, bias=True)
(norm1): LayerNorm((28,), eps=1e-05, elementwise_affine=True)
(norm2): LayerNorm((28,), eps=1e-05, elementwise_affine=True)
(dropout1): Dropout(p=0.1, inplace=False)
(dropout2): Dropout(p=0.1, inplace=False)
)
)
)
)
Library/Python/3.7/lib/python/site-packages/torch/jit/init.py:1037: TracerWarning: Output nr 1. of the traced function does not match the corresponding output of the Python function. Detailed error:
Not within tolerance rtol=1e-05 atol=1e-05 at input[29, 0, 14] (1.3225914239883423 vs. -0.8373285531997681) and 838 other locations (99.88%)
check_tolerance, _force_outplace, True, _module_class)
Converting Frontend ==> MIL Ops: 0%| | 0/188 [00:00<?, ? ops/s]Converting op 25 : constant
Converting op 26 : size
Converting op 27 : constant
Converting op 28 : size
Converting op 29 : constant
Converting op 30 : size
Converting op embed_dim.1 : numtotensor
Converting op 32 : constant
Converting op head_dim.1 : floor_divide
Converting op 34 : int
Converting op 35 : int
Converting op 36 : int
Converting op 37 : t
Converting op output.1 : matmul
Converting op 39 : constant
Converting op 40 : add
Converting op 41 : constantchunk
Converting op 44 : constant
Converting op q.2 : mul
Converting op 46 : constant
Converting op q.3 : contiguous
Converting op 48 : listconstruct
Converting op 49 : view
Converting op 50 : constant
Converting op 51 : constant
Converting op q.4 : transpose
Converting op 53 : constant
Converting op 54 : contiguous
Converting op 55 : constant
Converting op 56 : listconstruct
Converting op 57 : view
Converting op 58 : constant
Converting op 59 : constant
Converting op k.2 : transpose
Converting op 61 : constant
Converting op 62 : contiguous
Converting op 63 : constant
Converting op 64 : listconstruct
Converting op 65 : view
Converting op 66 : constant
Converting op 67 : constant
Converting op v.2 : transpose
Converting op 69 : constant
Converting op 70 : constant
Converting op 71 : transpose
Converting op attn_output_weights.1 : bmm
Converting Frontend ==> MIL Ops: 24%|██████████████████████████████████████████████████████████▉ | 45/188 [00:00<00:00, 679.96 ops/s]
Traceback (most recent call last):
File "testTransformerEncoder.py", line 40, in
inputs= [ ct.TensorType(name="input1", shape=dummy_input.shape) ]
File "Library/Python/3.7/lib/python/site-packages/coremltools/converters/_converters_entry.py", line 299, in convert
**kwargs
File "Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/converter.py", line 120, in _convert
prog = frontend_converter(model, **kwargs)
File "Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/converter.py", line 62, in call
return load(*args, **kwargs)
File "Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 84, in load
raise e
File "Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 76, in load
prog = converter.convert()
File "Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 302, in convert
convert_nodes(self.context, self.graph)
File "Library/Python/3.7/lib/python/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 53, in convert_nodes
"PyTorch convert function for op {} not implemented".format(node.kind)
RuntimeError: PyTorch convert function for op bmm not implemented
The text was updated successfully, but these errors were encountered: