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:
MxAcclreplaces bothAsyncAcclandMultiStreamAsyncAccl. Single- and multi-stream applications now use the same class, connecting each stream withconnect_stream().MxAcclMTreplacesSyncAccl. It keeps the manual send/receive workflow and adds multi-stream support through an explicitstream_id.
C++ runtime. The class names stay the same, but three things change:
MxAccl::stop()is removed.The output callback now returns
voidinstead ofbool.MxAcclMTmethods now takestream_idbeforemodel_id, the same order asMxAccl.
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 |
|---|---|---|
|
|
|
|
|
Single-stream and multi-stream apps now use the same class. |
|
|
For multiple streams, use |
|
|
Renamed for consistency with the C++ API. |
|
|
A single call now connects both callbacks for a given stream. |
|
Call |
Each stream is connected explicitly with its own |
|
|
Streaming no longer begins implicitly; you must call |
|
(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_callbacknow always takes astream_idargument, even for a single stream.out_callbackreceives a list of output feature maps (mxa_output) followed by the stream ID (stream_id). Remove the*beforemxa_outputin 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 |
|---|---|---|
|
|
You can still pass the DFP path as the first positional argument. |
|
|
Renamed for consistency with the C++ API. |
|
|
Renamed for consistency with the C++ API. |
|
|
A |
|
|
A |
|
|
A |
|
(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 |
|---|---|---|
|
(removed) |
The runtime stops streams and cleans up on its own. Delete the call. |
|
|
The output callback no longer returns a flag. Only the input callback decides when a stream ends. |
|
|
|
|
|
Same swap as |
|
|
Same swap as |
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 useMxAcclfor both single- and multi-stream applications.Use
connect_post_model()/connect_pre_model()instead ofset_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)andout_callback(self, mxa_output, stream_id).Call
start()beforewait();stop()is no longer required.
Python: for SyncAccl → MxAcclMT:
Import the new class with
from memryx import MxAcclMT, and use it in place ofSyncAccl.Use
connect_post_model()/connect_pre_model()instead ofset_postprocessing_model()/set_preprocessing_model().Replace
send()withsend_input()andreceive()withreceive_output(). Pass astream_idto both methods and torun(). If you specify a model by keyword, usemodel_id. Bothreceive_output()andrun()return a list of output arrays.Drop the
shutdown()call; it is no longer required.
C++: for MxAccl / MxAcclMT:
Delete every
stop()call. Usewait()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(), passstream_idbeforemodel_id. Old code still compiles, so check every call by hand.
