nimbudew
nimbudew

Reputation: 978

CoreML: Unable to perform matrix multiplication

I'm trying to implement a matrix multiplication operation in my network using NetworkBuilder. I wish to multiply two tensors of size (20x50) and (50x100) to get a tensor of size (20x100).

How can I do that? I've tried to use add_batched_mat_mul but I'm getting the below error on coremltools==3.0b3 and coremltools==3.0b4

How can I perform a matmul operation with the above mentioned tensor dimensions?

Error on coremltools==3.0b3

RuntimeWarning: You will not be able to run predict() on this Core ML model. Underlying exception message was: Error compiling model: "Error reading protobuf spec. validator error: Unsupported layer type (CoreML.Specification.NeuralNetworkLayer) for layer 'matmul'.".
  RuntimeWarning)

Error on coremltools==3.0b4

  File "test2.py", line 28, in <module>
    out = model.predict({"matrix_left": np.zeros((20, 50, 1))})
  File "python2.7/site-packages/coremltools/models/model.py", line 345, in predict
    raise Exception('Unable to load CoreML.framework. Cannot make predictions.')
Exception: Unable to load CoreML.framework. Cannot make predictions.
exception loading model proxy: dlopen(python2.7/site-packages/coremltools/libcoremlpython.so, 2): Symbol not found: _objc_opt_class
  Referenced from: python2.7/site-packages/coremltools/libcoremlpython.so (which was built for Mac OS X 10.15)
  Expected in: /usr/lib/libobjc.A.dylib
 in python2.7/site-packages/coremltools/libcoremlpython.so

Script used:

import coremltools.models.datatypes as datatypes
from coremltools.models.neural_network import NeuralNetworkBuilder
from coremltools.models import MLModel

import numpy as np

model_input_features = [
    ("matrix_left", datatypes.Array(20, 50, 1)),
]
model_output_features = [
    ("y", datatypes.Array(20, 100, 1)),
]

builder = NeuralNetworkBuilder(input_features=model_input_features, output_features=model_output_features)

np.random.seed(42)

matrix_right = np.random.rand(50, 100, 1)
builder.add_load_constant(name="matrix_right", output_name="y",
                          constant_value=matrix_right, shape=(50, 100, 1))


builder.add_batched_mat_mul(name="matmul", input_names=["matrix_left", "matrix_right"],
                            output_name="y")

model = MLModel(builder.spec)
out = model.predict({"matrix_left": np.zeros((20, 50, 1))})
y = out["y"]
print(y)
print(y.shape)

I've also tried using dot product using add_elementwise, but get the following error:

RuntimeWarning: You will not be able to run predict() on this Core ML model. 
Underlying exception message was: Error compiling model: "compiler error:  Dot product layer: 'matmul': 
height dimension of the input blob must be 1.".

Script:

matrix_right = np.random.rand(50, 100, 1)
builder.add_load_constant(name="matrix_right", output_name="matrix_right", constant_value=matrix_right, shape=(50, 100, 1))
builder.add_elementwise("matmul", input_names=["matrix_left", "matrix_right"], output_name="y", mode="DOT")

Upvotes: 0

Views: 664

Answers (1)

Matthijs Hollemans
Matthijs Hollemans

Reputation: 7892

Try this:

builder.add_load_constant(name="matrix_right", output_name="matrix_right",
                      constant_value=matrix_right, shape=(50, 100, 1))

Note that the output name is now "matrix_right" instead of "y".

The model still doesn't work with 3.0b3 or 3.0b4 but at least it's a valid model now. :-)

Upvotes: 1

Related Questions