Skip to content

Commit c17a89b

Browse files
committed
[mrvl][runtime]: Support Marvell Hardware Runtime
Signed-off-by: Krishna Bindumadhavan <[email protected]> Change-Id: Id9052552fbb3f19a53462183967d30679f3aa286
1 parent e3e27f5 commit c17a89b

File tree

5 files changed

+555
-7
lines changed

5 files changed

+555
-7
lines changed

cmake/modules/contrib/Mrvl.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ if(USE_MRVL)
2020
message(STATUS "Build with Mrvl support")
2121
file(GLOB RUNTIME_MRVL_SRCS
2222
src/runtime/contrib/mrvl/mrvl_runtime.cc
23+
src/runtime/contrib/mrvl/mrvl_hw_runtime.cc
2324
src/runtime/contrib/mrvl/mrvl_sw_runtime_lib.cc
2425
)
2526
list(APPEND RUNTIME_SRCS ${RUNTIME_MRVL_SRCS})

docs/how_to/deploy/mrvl.rst

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ integrated MLIP cn10ka processor, using only 4 tiles in the block.
100100
python3 -m tvm.driver.tvmc compile --target="mrvl, llvm" \
101101
--target-llvm-mtriple=aarch64-linux-gnu --target-llvm-mcpu=neoverse-n2 \
102102
--target-mrvl-num_tiles=4 \
103+
--target-mrvl-mattr="hw -quantize=fp16 -wb_pin_ocm=1" \
103104
--cross-compiler aarch64-linux-gnu-gcc \
104105
--output model.tar \
105106
mnist-12.onnx
106107
107-
The runtime support for hardware acceleration is a WIP, it will be added in future PR.
108108
109109
3.3. TVMC Compiler: mrvl specific Command Line Options
110110
------------------------------------------------------
@@ -125,7 +125,7 @@ The runtime support for hardware acceleration is a WIP, it will be added in futu
125125
Maximum number of tiles that may be used, possible values = {1,2,4,8}, defaults to 8
126126

127127
* mattr:
128-
Attributes for mrvl; possible values = {quantize, wb_pin_ocm}
128+
Attributes for mrvl; possible values = {quantize, wb_pin_ocm, run_mode}
129129

130130
mattr specifies the data type, code generation options and optimizations.
131131

@@ -141,15 +141,23 @@ The runtime support for hardware acceleration is a WIP, it will be added in futu
141141
Optimize runtime by preloading a model's weights and bias into
142142
the on chip memory. Possible values = {0, 1}. Default is 0 (no preload)
143143

144-
4. Compile ONNX model for Simulator + LLVM / x86_64 target
145-
----------------------------------------------------------
144+
**3. run_mode**
145+
146+
Specify whether to compile for the simulator or for the target hardware (Octeon).
147+
Possible values = {sim, hw}. Default is sim (software simulator).
148+
149+
4. Compile ONNX model using the TVMC flow
150+
-----------------------------------------
146151

147152
In the TVMC mrvl flow, the model is partitioned into Marvell and LLVM regions.
148153
Building each partitioned Marvell subgraph generates serialized nodes.json and
149154
const.json. Partitioned nodes.json is the representation of the model graph which is
150155
suitable for the Marvell compiler (mrvl-tmlc). The compiler compiles the model graph to
151156
generate the model binary with MLIP instructions.
152157

158+
4.1 Compile and Run ONNX model for Simulator + LLVM / x86_64 target
159+
--------------------------------------------------------------------
160+
153161
**Model Compilation for Simulator + LLVM / x86_64 target**
154162

155163
.. code:: python
@@ -165,6 +173,23 @@ Generated model binary is simulated using Marvell's MLIP Simulator(mrvl-mlsim).
165173
166174
python3 -m tvm.driver.tvmc run --inputs infer.npz --outputs predict.npz model.tar --number=0
167175
176+
4.2 Compile and Run ONNX model for Octeon target
177+
----------------------------------------------------------
178+
179+
**Model Compilation for Octeon target**
180+
181+
Please refer to section 3.2 for the example command line.
182+
183+
**Run TVM models on the Octeon Target**
184+
185+
The cross compiled binary can be run on the target hardware using the tvmc run command.
186+
Alternatively, the RPC flow enables remote execution on the target device from your
187+
local machine: https://tvm.apache.org/docs/how_to/tutorials/cross_compilation_and_rpc.html
188+
189+
.. code:: python
190+
191+
python3 -m tvm.driver.tvmc run --inputs infer.npz --outputs predict.npz model.tar
192+
168193
5. Compiling a model using Python APIs
169194
--------------------------------------
170195

python/tvm/relay/op/contrib/mrvl.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ def add_attributes(mod, annotate_target_str, **kwargs):
272272
mod : module with attributes
273273
"""
274274
working_dir = mrvl_contrib.get_working_dir()
275+
sim_attr_found = False
276+
hw_attr_found = False
275277

276278
if "mattr" in kwargs:
277279
base_opts_str = kwargs.get("mattr")
@@ -286,6 +288,14 @@ def add_attributes(mod, annotate_target_str, **kwargs):
286288
if "wb_pin_ocm" not in base_opts_str:
287289
base_opts_str = f"{base_opts_str} -wb_pin_ocm=0"
288290

291+
if "sim" in base_opts_str:
292+
sim_attr_found = True
293+
base_opts_str = base_opts_str.replace("sim", "")
294+
295+
if "hw" in base_opts_str:
296+
hw_attr_found = True
297+
base_opts_str = base_opts_str.replace("hw", "")
298+
289299
else:
290300
base_opts_str = "-arch=mlip -quantize=fp16 -wb_pin_ocm=0"
291301

@@ -294,13 +304,20 @@ def add_attributes(mod, annotate_target_str, **kwargs):
294304
elif "num_tiles" not in base_opts_str:
295305
base_opts_str = f"{base_opts_str} -num_tiles=8"
296306

307+
mode_string = "sim"
308+
if sim_attr_found:
309+
mode_string = "sim"
310+
elif hw_attr_found:
311+
mode_string = "hw"
312+
297313
for var in mod.get_global_vars():
298314
func_name = var.name_hint
299315
func = mod[func_name]
300316

301317
if annotate_target_str in func_name:
302318
func = func.with_attr("working_dir", working_dir)
303319
func = func.with_attr("compiler_opts_string", base_opts_str)
320+
func = func.with_attr("mode", mode_string)
304321
mod.update_func(var, func)
305322

306323
return mod

src/relay/backend/contrib/mrvl/codegen.cc

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,7 @@ runtime::Module MrvlCompiler(const ObjectRef& ref) {
14671467

14681468
Function func = Downcast<Function>(ref);
14691469
std::string func_name = backend::GetExtSymbol(func);
1470+
const std::string mrvl_run_mode = func->GetAttr<String>("mode").value();
14701471
runtime::Module runtime_lib;
14711472

14721473
// Extract attributes from the frontend to be passed to the runtime
@@ -1485,13 +1486,32 @@ runtime::Module MrvlCompiler(const ObjectRef& ref) {
14851486
std::string modified_json = (*modifyConsts)(nodes_json_string, consts_json_string);
14861487
auto json_vec = split(modified_json, '|');
14871488

1489+
// Extract attributes from the nodes_json by key-value lookup using Python API
1490+
// These are passed to hardware runtime module for initialization
1491+
const tvm::runtime::PackedFunc* json_lookup;
1492+
json_lookup = runtime::Registry::Get("tvm.mrvl.find_value_in_KV_pair");
1493+
const std::string string_inp = (*json_lookup)(nodes_json_string, "num_subgraph_inputs");
1494+
const int num_inputs = std::stoi(string_inp);
1495+
const std::string string_out = (*json_lookup)(nodes_json_string, "num_subgraph_outputs");
1496+
const int num_outputs = std::stoi(string_out);
1497+
const std::string string_bsize = (*json_lookup)(nodes_json_string, "batch_size");
1498+
const int batch_size = std::stoi(string_bsize);
1499+
14881500
// Invoke Marvell Backend compiler to generate binary for sub graph
14891501
const auto* compile = runtime::Registry::Get("tvm.mrvl.CompileModel");
14901502
std::string bin = (*compile)(func_name, json_vec[0], json_vec[1], compiler_opt);
14911503

1492-
const auto* pf = runtime::Registry::Get("runtime.mrvl_runtime_create");
1493-
ICHECK(pf != nullptr) << "Cannot find software simulator runtime module to create";
1494-
runtime_lib = (*pf)(func_name, json_vec[0], bin);
1504+
if (mrvl_run_mode == "sim") {
1505+
const auto* pf = runtime::Registry::Get("runtime.mrvl_runtime_create");
1506+
ICHECK(pf != nullptr) << "Cannot find software simulator runtime module to create";
1507+
runtime_lib = (*pf)(func_name, json_vec[0], bin);
1508+
} else if (mrvl_run_mode == "hw") {
1509+
const auto* pf = runtime::Registry::Get("runtime.mrvl_hw_runtime_create");
1510+
ICHECK(pf != nullptr) << "Cannot find hardware runtime module to create";
1511+
runtime_lib = (*pf)(func_name, json_vec[0], bin, num_inputs, num_outputs, batch_size);
1512+
} else {
1513+
ICHECK(0) << "Unrecognized Marvell Run Mode! " << mrvl_run_mode;
1514+
}
14951515

14961516
return runtime_lib;
14971517
}

0 commit comments

Comments
 (0)