Migrating to SDK 3.0#

Introduction#

This tutorial explains how to migrate Python and C++ applications from the SDK 2.x runtime APIs to SDK 3.0.

Python runtime. The Python API now mirrors the C++ Accelerator API. Both languages share the same classes, method names, and stream model, so what you learn in one carries over to the other. Two classes replace the previous Python API:

  • MxAccl replaces both AsyncAccl and MultiStreamAsyncAccl. Single- and multi-stream applications now use the same class, connecting each stream with connect_stream().

  • MxAcclMT replaces SyncAccl. It keeps the manual send/receive workflow and adds multi-stream support through an explicit stream_id.

C++ runtime. The class names stay the same, but three things change:

  • MxAccl::stop() is removed.

  • The output callback now returns void instead of bool.

  • MxAcclMT methods now take stream_id before model_id, the same order as MxAccl.

Jump to the section that matches your application:

Python: Migrating AsyncAccl / MultiStreamAsyncAccl to MxAccl#

In SDK 3.0, MxAccl handles both single-stream and multi-stream applications. Use connect_stream() to register the input and output callbacks for each stream, as in the C++ Accelerator API.

What’s Changed#

Old (SDK 2.x)

New (SDK 3.0)

Notes

from memryx import AsyncAccl, MultiStreamAsyncAccl

from memryx import MxAccl

MxAccl is exported directly from the memryx package.

AsyncAccl(dfp)

MxAccl(dfp)

Single-stream and multi-stream apps now use the same class.

MultiStreamAsyncAccl(dfp, stream_workers=n)

MxAccl(dfp)

For multiple streams, use MxAccl and omit the stream_workers argument.

accl.set_postprocessing_model(path)

accl.connect_post_model(path)

Renamed for consistency with the C++ API. connect_pre_model() is also available for pre-processing models.

accl.connect_input(in_cb) / accl.connect_output(out_cb)

accl.connect_stream(in_cb, out_cb, stream_id)

A single call now connects both callbacks for a given stream.

accl.connect_streams(in_cb, out_cb, num_streams)

Call accl.connect_stream(in_cb, out_cb, i) once per stream, in a loop

Each stream is connected explicitly with its own stream_id.

accl.wait()

accl.start() followed by accl.wait()

Streaming no longer begins implicitly; you must call start() before wait().

accl.stop()

(removed)

No longer needed; the runtime cleans up automatically.

Callback signatures also change slightly, as covered in 4. Update the Callback Signatures below.

Migration Steps#

1. Update the Import#

from memryx import AsyncAccl, MultiStreamAsyncAccl
from memryx import MxAccl

2. Replace the Accelerator Class#

Create one MxAccl instance for the DFP, regardless of how many streams your application uses.

if num_streams == 1:
    accl = AsyncAccl(dfp_path)
else:
    accl = MultiStreamAsyncAccl(dfp_path, stream_workers=num_streams)
accl = MxAccl(dfp_path)

3. Connect the Post-Processing Model#

accl.set_postprocessing_model(post_model_path)
accl.connect_post_model(post_model_path)

Note

If your application uses a preprocessing model, connect it with accl.connect_pre_model(pre_model_path).

4. Update the Callback Signatures#

With MxAccl, single-stream and multi-stream applications use the same callback signatures:

  • in_callback now always takes a stream_id argument, even for a single stream.

  • out_callback receives a list of output feature maps (mxa_output) followed by the stream ID (stream_id). Remove the * before mxa_output in the callback definition.

# single stream (AsyncAccl)
def in_callback(self):
    ...

def out_callback(self, *mxa_output):
    ...

# multiple streams (MultiStreamAsyncAccl)
def in_callback(self, stream_id):
    ...

def out_callback(self, stream_id, *mxa_output):
    ...
# single stream and multi-stream both use this signature (note the removed *)
def in_callback(self, stream_id):
    ...

def out_callback(self, mxa_output, stream_id):
    ...

5. Connect Streams and Start the Accelerator#

Connect each stream explicitly with connect_stream(), then call start() before wait().

if num_streams == 1:
    accl.connect_input(app.in_cb)
    accl.connect_output(app.out_cb)
else:
    accl.connect_streams(app.in_cb, app.out_cb, num_streams)

accl.wait()
for i in range(num_streams):
    accl.connect_stream(app.in_cb, app.out_cb, i)

accl.start()  # explicitly start the accelerator
accl.wait()

Full Example#

Putting it all together, a single-stream application migrates from:

from memryx import AsyncAccl

accl = AsyncAccl(dfp_path)
accl.set_postprocessing_model(post_model_path)
accl.connect_input(app.in_callback)
accl.connect_output(app.out_callback)
accl.wait()

# callback definitions
def in_callback(self):
    ...

def out_callback(self, *mxa_output):
    ...

to:

from memryx import MxAccl

accl = MxAccl(dfp_path)
accl.connect_post_model(post_model_path)
accl.connect_stream(app.in_callback, app.out_callback, stream_id=0)
accl.start()
accl.wait()

# callback definitions
def in_callback(self, stream_id):
    ...

def out_callback(self, mxa_output, stream_id):
    ...

And a multi-stream application migrates from:

from memryx import MultiStreamAsyncAccl

accl = MultiStreamAsyncAccl(dfp_path, stream_workers=num_streams)
accl.set_postprocessing_model(post_model_path)
accl.connect_streams(app.in_callback, app.out_callback, num_streams)
accl.wait()

# callback definitions
def in_callback(self, stream_idx):
    ...

def out_callback(self, stream_idx, *mxa_output):
    ...

to:

from memryx import MxAccl

accl = MxAccl(dfp_path)
accl.connect_post_model(post_model_path)

for i in range(num_streams):
    accl.connect_stream(app.in_callback, app.out_callback, stream_id=i)

accl.start()
accl.wait()

# callback definitions
def in_callback(self, stream_idx):
    ...

def out_callback(self, mxa_output, stream_idx):
    ...

Python: Migrating SyncAccl to MxAcclMT#

In SDK 3.0, MxAcclMT replaces SyncAccl for applications that send inputs and receive outputs directly. Pass a stream_id to select the stream for each operation.

What’s Changed#

Old (SDK 2.x)

New (SDK 3.0)

Notes

SyncAccl(dfp)

MxAcclMT(dfp)

You can still pass the DFP path as the first positional argument.

accl.set_preprocessing_model(path)

accl.connect_pre_model(path)

Renamed for consistency with the C++ API.

accl.set_postprocessing_model(path)

accl.connect_post_model(path)

Renamed for consistency with the C++ API.

accl.send(data, model_idx=0, timeout=None)

accl.send_input(data, stream_id, model_id=0, timeout=0)

A stream_id is now required, and model_idx is renamed to model_id. timeout=0 means “block indefinitely”, equivalent to the old timeout=None.

accl.receive(model_idx=0, timeout=None)

accl.receive_output(stream_id, model_id=0, timeout=0)

A stream_id is now required, model_idx is renamed to model_id, and the return value is always a List[np.ndarray] instead of a single array or tuple.

accl.run(inputs, model_idx=0)

accl.run(in_data, stream_id, model_id=0, timeout=0)

A stream_id is now required, model_idx is renamed to model_id, and the return value is always a List[np.ndarray].

accl.shutdown()

(removed)

No longer needed; the runtime cleans up automatically.

Migration Steps#

1. Update the Import and Accelerator Class#

from memryx import SyncAccl

accl = SyncAccl(dfp_path)
from memryx import MxAcclMT

accl = MxAcclMT(dfp_path)

2. Connect Pre/Post-Processing Models#

accl.set_preprocessing_model(pre_model_path)
accl.set_postprocessing_model(post_model_path)
accl.connect_pre_model(pre_model_path)
accl.connect_post_model(post_model_path)

3. Update send() / receive() Calls#

Every call now needs an explicit stream_id. Use 0 if your application only needs a single stream. If you pass a model index, rename the model_idx keyword to model_id.

accl.send(data)
output = accl.receive()  # single array, or a tuple for multi-output models
stream_id = 0
accl.send_input(data, stream_id)
outputs = accl.receive_output(stream_id)  # always a list of arrays

4. Update run() Calls#

output = accl.run(data)  # single array, or a tuple for multi-output models
outputs = accl.run(data, stream_id=0)  # always a list of arrays

5. Remove the shutdown() Call#

accl.shutdown()
# nothing to do - the runtime cleans up automatically

Full Example#

A typical send/receive loop migrates from:

from memryx import SyncAccl

accl = SyncAccl(dfp_path)
accl.set_postprocessing_model(post_model_path)

for data in dataset:
    accl.send(data)
    output = accl.receive()
    process(output)

accl.shutdown()

to:

from memryx import MxAcclMT

accl = MxAcclMT(dfp_path)
accl.connect_post_model(post_model_path)

stream_id = 0
for data in dataset:
    accl.send_input(data, stream_id)
    outputs = accl.receive_output(stream_id)
    process(outputs)

C++: Updating MxAccl and MxAcclMT#

C++ applications keep the same MxAccl and MxAcclMT classes. Only three small changes are needed.

What’s Changed#

Old (SDK 2.x)

New (SDK 3.0)

Notes

accl.stop()

(removed)

The runtime stops streams and cleans up on its own. Delete the call.

bool out_cb(std::vector<const MX::Types::FeatureMap*>, int stream_id)

void out_cb(std::vector<const MX::Types::FeatureMap*>, int stream_id)

The output callback no longer returns a flag. Only the input callback decides when a stream ends.

send_input(in_data, model_id, stream_id, timeout)

send_input(in_data, stream_id, model_id = 0, timeout = 0)

stream_id now comes first, same as MxAccl::connect_stream(). model_id now defaults to 0.

receive_output(out_data, model_id, stream_id, timeout)

receive_output(out_data, stream_id, model_id = 0, timeout = 0)

Same swap as send_input().

run(in_data, out_data, model_id, stream_id, timeout)

run(in_data, out_data, stream_id, model_id = 0, timeout = 0)

Same swap as send_input().

Migration Steps#

1. Remove the stop() Call#

MxAccl::stop() is gone. You don’t need it:

  • A stream ends when its input callback returns false.

  • wait() blocks until every stream has ended.

  • The destructor releases everything that is left.

Delete every stop() call. If you called stop() to wait for the streams to finish, call wait() instead.

accl.start();
accl.wait();
accl.stop();
accl.start();
accl.wait();

2. Make the Output Callback Return void#

In SDK 2.x, the input and output callbacks shared one signature, so the output callback also had to return a bool. Only the input callback signals the end of a stream by returning false. In SDK 3.0, the output callback returns void.

Change the output callback’s return type to void and remove the final return true;. If the callback exits early, use return;. The input callback keeps its existing signature.

// input callback: unchanged
bool in_cb(std::vector<const MX::Types::FeatureMap*> dst, int stream_id) {
    // fill the input, or return false to end this stream ...
    return true;
}

// output callback: returns bool
bool out_cb(std::vector<const MX::Types::FeatureMap*> src, int stream_id) {
    // process the output ...
    return true;
}
// input callback: unchanged
bool in_cb(std::vector<const MX::Types::FeatureMap*> dst, int stream_id) {
    // fill the input, or return false to end this stream ...
    return true;
}

// output callback: now returns void
void out_cb(std::vector<const MX::Types::FeatureMap*> src, int stream_id) {
    // process the output ...
}

Note

If you store the output callback in a std::function<bool(...)>, change it to std::function<void(...)>, or use the new MX::Types::out_callback_t type. The input callback type is MX::Types::in_callback_t.

3. Swap stream_id and model_id in MxAcclMT Calls#

MxAcclMT now takes stream_id before model_id, the same order as MxAccl::connect_stream(). This applies to send_input(), receive_output(), and run(). model_id now defaults to 0, so single-model applications can leave it out.

Warning

Both arguments are integers, so a call that passes them in the old order can still compile. Check the argument order in each call to send_input(), receive_output(), and run().

accl.send_input(ifmaps, model_id, stream_id);
accl.receive_output(ofmaps, model_id, stream_id);
accl.run(ifmaps, ofmaps, model_id, stream_id);
accl.send_input(ifmaps, stream_id, model_id);
accl.receive_output(ofmaps, stream_id, model_id);
accl.run(ifmaps, ofmaps, stream_id, model_id);

// single-model DFP: model_id can be omitted
accl.send_input(ifmaps, stream_id);
accl.receive_output(ofmaps, stream_id);

Full Example#

An MxAccl application migrates from:

bool in_cb(std::vector<const MX::Types::FeatureMap*> dst, int stream_id) {
    if (!get_next_frame(frame))
        return false;  // ends this stream
    dst[0]->set_data(frame);
    return true;
}

bool out_cb(std::vector<const MX::Types::FeatureMap*> src, int stream_id) {
    src[0]->get_data(output);
    process(output);
    return true;
}

MX::Runtime::MxAccl accl(dfp_path);
accl.connect_stream(in_cb, out_cb, 0);
accl.start();
accl.wait();
accl.stop();

to:

bool in_cb(std::vector<const MX::Types::FeatureMap*> dst, int stream_id) {
    if (!get_next_frame(frame))
        return false;  // ends this stream
    dst[0]->set_data(frame);
    return true;
}

void out_cb(std::vector<const MX::Types::FeatureMap*> src, int stream_id) {
    src[0]->get_data(output);
    process(output);
}

MX::Runtime::MxAccl accl(dfp_path);
accl.connect_stream(in_cb, out_cb, 0);
accl.start();
accl.wait();

And an MxAcclMT send/receive loop migrates from:

MX::Runtime::MxAcclMT accl(dfp_path);

int model_id = 0;
int stream_id = 0;
while (get_next_frame(frame)) {
    accl.send_input({frame}, model_id, stream_id);
    accl.receive_output(ofmaps, model_id, stream_id);
    process(ofmaps);
}

to:

MX::Runtime::MxAcclMT accl(dfp_path);

int stream_id = 0;
while (get_next_frame(frame)) {
    accl.send_input({frame}, stream_id);
    accl.receive_output(ofmaps, stream_id);
    process(ofmaps);
}

Summary#

In this tutorial, we covered the changes needed to move an application to SDK 3.0.

Python: for AsyncAccl / MultiStreamAsyncAccl → MxAccl:

  • Import the new class with from memryx import MxAccl, and use MxAccl for both single- and multi-stream applications.

  • Use connect_post_model() / connect_pre_model() instead of set_postprocessing_model() / set_preprocessing_model().

  • Connect every stream explicitly with connect_stream(in_cb, out_cb, stream_id).

  • Update callback signatures: in_callback(self, stream_id) and out_callback(self, mxa_output, stream_id).

  • Call start() before wait(); stop() is no longer required.

Python: for SyncAccl → MxAcclMT:

  • Import the new class with from memryx import MxAcclMT, and use it in place of SyncAccl.

  • Use connect_post_model() / connect_pre_model() instead of set_postprocessing_model() / set_preprocessing_model().

  • Replace send() with send_input() and receive() with receive_output(). Pass a stream_id to both methods and to run(). If you specify a model by keyword, use model_id. Both receive_output() and run() return a list of output arrays.

  • Drop the shutdown() call; it is no longer required.

C++: for MxAccl / MxAcclMT:

  • Delete every stop() call. Use wait() if you need to block until the streams finish.

  • Change the output callback to return void. Only the input callback ends a stream.

  • In MxAcclMT::send_input() / receive_output() / run(), pass stream_id before model_id. Old code still compiles, so check every call by hand.