Skip to content
imaged

Work with tensors

Inspect model inputs, check a backend, and pass raw tensors.

Use your own .slab model and a runtime license. Model files carry their input sizes, preprocessing, labels, and backend preferences.

Run Python inside a signed macOS app with a customer runtime build, or use a development build.

"""Inspect a model, check a backend, then run it on a raw NumPy tensor.

usage: python tensors.py MODEL.slab LICENSE_FILE
"""

import sys

import numpy as np

import slab

model, license_file = sys.argv[1:3]

cfg = slab.Config(license=open(license_file).read())
session = slab.Session(cfg)

# Inspect reads the model's description without loading its weights.
session.inspect(model)
for t in session.inputs:
    print(f"input  {t.name}: {t.dtype.name} {t.shape}")
for t in session.outputs:
    print(f"output {t.name}: {t.dtype.name} {t.shape}")
print(f"estimated memory: {session.estimated_memory_bytes} bytes")
ok, reason = session.check_backend(slab.Backend.GPU)
print("GPU can run it" if ok else f"GPU cannot run it: {reason}")

session.open(model)
first = session.inputs[0]
inp = slab.Input()
inp.set_tensor(first.name, np.zeros(first.shape, dtype=np.float32))
out = session.run(inp)

y = out.numpy(0)
print(f"output 0: shape {y.shape}, max {y.max():.4f} at {int(y.argmax())}, on {out.backend_used.name}")