Skip to main content

prediction with tensorflow model being much slower than on tensorflow.js with a default configuration on a macbook M1

I'm likely doing something very wrong. Since the prediction was much slower on python I've written two, seemingly equivalent, snippets to test the two libraries.

In Python (Python 3.9.12):

from tensorflow.python.keras.layers import Dense, Flatten
from tensorflow.python.keras.models import Sequential
import time
from tensorflow.python.client import device_lib


def create_model():
    input_shape = (1, 300)
    model = Sequential()
    model.add(Dense(200, activation="elu", input_shape=input_shape))
    model.add(Flatten())
    model.add(Dense(50, activation="elu"))
    model.add(Dense(20, activation="linear"))
    model.compile()
    return model


if __name__ == '__main__':
    print(device_lib.list_local_devices())
    model = create_model()
    inp = [[[0.5] * 300]]

    # warmup
    for i in range(0, 100):
        model.predict(inp)

    start_time = time.time()
    for i in range(0, 100):
        model.predict(inp)

    end_time = time.time()
    print(f" {end_time - start_time} s")

In JS (running on node v19.6.1):

import * as tf from "@tensorflow/tfjs-node";


function createModel(): tf.LayersModel {
    const model = tf.sequential();
    const inputShape = [1, 300]
    model.add(tf.layers.dense({activation: "elu", units: 200, inputShape: inputShape}));
    model.add(tf.layers.flatten())
    model.add(tf.layers.dense({activation: "elu", units: 50}));
    model.add(tf.layers.dense({activation: "linear", units: 20}));
    return model
}


const be = tf.getBackend()
console.log(be)
const model = createModel()
const inp = tf.tensor([[new Array(300).fill(0.5)]])
//warmup
for (let i = 0; i < 100; i++) {
    model.predict(inp)
}

console.time("p time");
for (let i = 0; i < 100; i++) {
    model.predict(inp)
}
console.timeEnd("p time")

The relative output for python:

python benchmark.py

[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 11057909948552266533
xla_global_id: -1
]
2023-03-26 21:54:03.426043: W tensorflow/tsl/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz
 3.1207358837127686 s

and node:

> ts-node  src/benchmark.ts

tensorflow
p time: 32.62ms

These are the specs of the mac:

Hardware Overview:

  Model Name:   MacBook Pro
  Model Identifier: MacBookPro18,1
  Chip: Apple M1 Pro
  Total Number of Cores:    10 (8 performance and 2 efficiency)
  Memory:   16 GB
  System Firmware Version:  7429.61.2
  OS Loader Version:    7429.61.2
  Serial Number (system):   WGR2WKVPX6
  Hardware UUID:    743BE62E-85CA-5B60-BA83-2F4EB16926CB
  Provisioning UDID:    00006000-0014690E22A1801E
  Activation Lock Status:   Disabled

I could understand a small difference but not a gap of 100 times. Any pointers?

I would have expected the execution to take a comparable amount of time with both libraries or tensorflow being faster.



source https://stackoverflow.com/questions/75850282/prediction-with-tensorflow-model-being-much-slower-than-on-tensorflow-js-with-a

Comments