Skip to content

Commit

Permalink
Update flash image option to weights
Browse files Browse the repository at this point in the history
  • Loading branch information
panickal-xmos committed Oct 26, 2023
1 parent d4365b1 commit 0965293
Show file tree
Hide file tree
Showing 21 changed files with 234 additions and 53 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ xf.convert("example_int8_model.tflite", "xcore_optimised_int8_model.tflite", {
})
```

To create a parameters file and a tflite model suitable for loading to flash, use the "xcore-flash-image-file" option.
To create a parameters file and a tflite model suitable for loading to flash, use the "xcore-weights-file" option.
```python
xf.convert("example_int8_model.tflite", "xcore_optimised_int8_flash_model.tflite", {
"xcore-flash-image-file ": "./xcore_params.params",
"xcore-weights-file ": "./xcore_params.params",
})
```

Expand Down
4 changes: 2 additions & 2 deletions docs/rst/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Name of the file where to place the optimized TFLITE model
Number of threads to translate for (max=5). Defaults to 1.


``xcore-flash-image-file filename``
``xcore-weights-file filename``
+++++++++++++++++++++++++++++++++++

File to place the learned parameters in. If this option is not specified,
Expand All @@ -86,7 +86,7 @@ be slower but allows large numbers of learned parameters to be used.
Sets a threshold under which to not place learned parameters in flash. The
default is set to 96 bytes. If less than 96 bytes, the overhead of lowering to flash is
more than the benefit gained. This option is only meaningful if
``xcore-flash-image-file`` has been used. You can experiment with this
``xcore-weights-file`` has been used. You can experiment with this
parameter to get a different trade-off between speed and memory requirements.

``xcore-reduce-memory true|false``
Expand Down
2 changes: 1 addition & 1 deletion examples/app_flash_single_model/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Please consult `here <../../docs/rst/flow.rst>`_ on how to install the tools.

In order to compile and run this example follow these steps::

xcore-opt --xcore-flash-image-file=model.params vww_quant.tflite -o model.tflite
xcore-opt --xcore-weights-file=model.params vww_quant.tflite -o model.tflite
mv model.tflite.cpp model.tflite.h src
xmake
python -c 'from xmos_ai_tools import xformer as xf; xf.generate_flash(
Expand Down
4 changes: 2 additions & 2 deletions examples/app_flash_two_models/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ sets of learned parameters into a single flash image.

In order to compile and run this example follow these steps::

xcore-opt --xcore-flash-image-file=model1.params \
xcore-opt --xcore-weights-file=model1.params \
--xcore-naming-prefix=model1_ \
vww_quant1.tflite -o model1.tflite
xcore-opt --xcore-flash-image-file=model2.params \
xcore-opt --xcore-weights-file=model2.params \
--xcore-naming-prefix=model2_ \
vww_quant2.tflite -o model2.tflite
mv model1.tflite.cpp model1.tflite.h src
Expand Down
4 changes: 2 additions & 2 deletions examples/app_flash_two_models_one_arena/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ The differences with ``app_flash_two_models`` example are minimal:

In order to compile and run this example follow these steps::

xcore-opt --xcore-flash-image-file=model1.params \
xcore-opt --xcore-weights-file=model1.params \
--xcore-naming-prefix=model1_ \
vww_quant1.tflite -o model1.tflite
xcore-opt --xcore-flash-image-file=model2.params \
xcore-opt --xcore-weights-file=model2.params \
--xcore-naming-prefix=model2_ \
vww_quant2.tflite -o model2.tflite
mv model1.tflite.cpp model1.tflite.h src
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
TFLITE_MODEL_PATH,
OPT_MODEL_PATH,
{
"xcore-flash-image-file": OPT_PARAMS_PATH,
"xcore-weights-file": OPT_PARAMS_PATH,
"xcore-thread-count": "5",
"xcore-naming-prefix": NAMING_PREFIX,
"xcore-op-split-tensor-arena": "True",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
TFLITE_MODEL_PATH,
OPT_MODEL_PATH,
{
"xcore-flash-image-file": OPT_PARAMS_PATH,
"xcore-weights-file": OPT_PARAMS_PATH,
"xcore-thread-count": "5",
"xcore-naming-prefix": NAMING_PREFIX,
},
Expand Down
4 changes: 2 additions & 2 deletions python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ def finalize_options(self):
"Intended Audience :: Science/Research",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Topic :: Scientific/Engineering",
"Topic :: Scientific/Engineering :: Mathematics",
"Topic :: Scientific/Engineering :: Artificial Intelligence",
"Topic :: Software Development",
"Topic :: Software Development :: Libraries",
"Topic :: Software Development :: Libraries :: Python Modules",
],
python_requires=">=3.7",
python_requires=">=3.8",
packages=find_namespace_packages(),
install_requires=REQUIRED_PACKAGES,
package_data=package_files,
Expand Down
6 changes: 5 additions & 1 deletion python/xmos_ai_tools/runtime/buildfiles/aitoolslib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ set(XMOS_AITOOLSLIB_DEFINITIONS
"NO_INTERPRETER"
)

set(XMOS_AITOOLSLIB_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/../lib/libxtflitemicro.a")
if(NOT ${CMAKE_SYSTEM_PROCESSOR} STREQUAL XCORE_XS3A)
set(XMOS_AITOOLSLIB_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/../lib/libhost_xtflitemicro.a")
else()
set(XMOS_AITOOLSLIB_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/../lib/libxtflitemicro.a")
endif()
set(XMOS_AITOOLSLIB_INCLUDES "${CMAKE_CURRENT_LIST_DIR}/../include")
4 changes: 2 additions & 2 deletions xformer/Test/loadconstantop.mlir
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: xcore-opt --mlir-io %s --xcore-apply-loadconstantop-patterns --xcore-flash-image-file=/dev/null --xcore-load-externally-if-larger=0 | FileCheck %s
// RUN: xcore-opt --mlir-io %s --xcore-apply-loadconstantop-patterns --xcore-weights-file=/dev/null --xcore-load-externally-if-larger=0 | FileCheck %s

// RUN: xcore-opt --mlir-io %s --xcore-apply-loadconstantop-patterns --xcore-flash-image-file=/dev/null --xcore-load-externally-if-larger=16 | FileCheck %s -check-prefix=LARGER-CHECK
// RUN: xcore-opt --mlir-io %s --xcore-apply-loadconstantop-patterns --xcore-weights-file=/dev/null --xcore-load-externally-if-larger=16 | FileCheck %s -check-prefix=LARGER-CHECK

// CHECK-LABEL: valid
func.func @valid(%arg0: tensor<?x4x8x1x!quant.uniform<i8:f32, 0.0078160231932997704>>) -> tensor<?x32x!quant.uniform<i8:f32, 0.037329975515604019:-13>> attributes {tf.entry_function = {inputs = "flatten_input", outputs = "Identity"}} {
Expand Down
2 changes: 1 addition & 1 deletion xformer/Test/loadflashop.mlir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: xcore-opt --mlir-io %s --xcore-write-flash-image --xcore-flash-image-file=/dev/null | FileCheck %s
// RUN: xcore-opt --mlir-io %s --xcore-write-flash-image --xcore-weights-file=/dev/null | FileCheck %s

// CHECK-LABEL: valid
func.func @valid(%arg0: tensor<?x4x8x1x!quant.uniform<i8:f32, 0.0078160231932997704>>) -> tensor<?x32x!quant.uniform<i8:f32, 0.037329975515604019:-13>> attributes {tf.entry_function = {inputs = "flatten_input", outputs = "Identity"}} {
Expand Down
2 changes: 1 addition & 1 deletion xformer/Transforms/ApplyLoadConstantOpPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ bool isNotUsedByLoadConstantOp(Value result) {

void ApplyLoadConstantOpPatterns::runOnOperation() {
func::FuncOp f = getOperation();
if (flashImageFilenameOption.empty()) {
if (weightsFilenameOption.empty()) {
f.emitError("Flash image file option should be provided to run this pass!");
signalPassFailure();
return;
Expand Down
2 changes: 1 addition & 1 deletion xformer/Transforms/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace xcore {

extern llvm::cl::opt<bool> enableBetaFloatOption;
extern llvm::cl::opt<unsigned> threadCountOption;
extern llvm::cl::opt<std::string> flashImageFilenameOption;
extern llvm::cl::opt<std::string> weightsFilenameOption;
extern llvm::cl::opt<unsigned> loadExternallyIfLargerOption;
extern llvm::cl::opt<bool> tileLoadOption;
extern llvm::cl::opt<unsigned> maxLoadExternalSizeOption;
Expand Down
4 changes: 2 additions & 2 deletions xformer/Transforms/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ void buildXCorePassPipeline(OpPassManager &pm) {
pm.addPass(createReplaceConv2DPass());
pm.addPass(createApplyXCPatternsPass());
// Add to pipeline only if flash image file option is provided
if (!flashImageFilenameOption.empty()) {
if (!weightsFilenameOption.empty()) {
pm.addPass(createApplyLoadConstantOpPatternsPass());
pm.addPass(createWriteFlashImagePass());
pm.addPass(createWriteWeightsPass());
}
// Run canonicalization, which includes combining Reshapes
pm.addPass(mlir::createCanonicalizerPass());
Expand Down
2 changes: 1 addition & 1 deletion xformer/Transforms/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ std::unique_ptr<OperationPass<func::FuncOp>> createReplaceConv2DPass();
std::unique_ptr<OperationPass<func::FuncOp>> createApplyXCPatternsPass();
std::unique_ptr<OperationPass<func::FuncOp>>
createApplyLoadConstantOpPatternsPass();
std::unique_ptr<OperationPass<func::FuncOp>> createWriteFlashImagePass();
std::unique_ptr<OperationPass<func::FuncOp>> createWriteWeightsPass();
std::unique_ptr<OperationPass<func::FuncOp>> createPlanMemoryPass();
std::unique_ptr<OperationPass<func::FuncOp>> createTranslateToCustomOpPass();

Expand Down
32 changes: 16 additions & 16 deletions xformer/Transforms/WriteFlashImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace xcore {

namespace {
// Write flash image
struct WriteFlashImage
: public PassWrapper<WriteFlashImage, OperationPass<func::FuncOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(WriteFlashImage)
struct WriteWeights
: public PassWrapper<WriteWeights, OperationPass<func::FuncOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(WriteWeights)

void getDependentDialects(DialectRegistry &registry) const final {
registry.insert<XCoreDialect>();
Expand All @@ -29,9 +29,9 @@ struct WriteFlashImage
void runOnOperation() override;
};

struct WriteFlashImagePattern : public OpRewritePattern<LoadConstantOp> {
WriteFlashImagePattern(std::vector<std::vector<char>> *tensorsVec,
MLIRContext *context)
struct WriteWeightsPattern : public OpRewritePattern<LoadConstantOp> {
WriteWeightsPattern(std::vector<std::vector<char>> *tensorsVec,
MLIRContext *context)
: OpRewritePattern<LoadConstantOp>(context), tensorsVec_(tensorsVec) {}

std::vector<char> getTensorData(LoadConstantOp loadOp) const {
Expand Down Expand Up @@ -129,9 +129,9 @@ struct WriteFlashImagePattern : public OpRewritePattern<LoadConstantOp> {
std::vector<std::vector<char>> *tensorsVec_;
};

void WriteFlashImage::runOnOperation() {
void WriteWeights::runOnOperation() {
func::FuncOp f = getOperation();
if (flashImageFilenameOption.empty()) {
if (weightsFilenameOption.empty()) {
f.emitError("Flash image file option should be provided to run this pass!");
signalPassFailure();
return;
Expand All @@ -143,33 +143,33 @@ void WriteFlashImage::runOnOperation() {
// with a LoadFlashOp
std::vector<std::vector<char>> tensorsVec;
RewritePatternSet patterns(ctx);
patterns.insert<WriteFlashImagePattern>(&tensorsVec, ctx);
patterns.insert<WriteWeightsPattern>(&tensorsVec, ctx);
(void)applyPatternsAndFoldGreedily(func, std::move(patterns));

if (tileLoadOption) {
if (failed(utils::writeTileServerDataToFile(flashImageFilenameOption,
if (failed(utils::writeTileServerDataToFile(weightsFilenameOption,
tensorsVec))) {
f.emitError("Failed to write tile data!");
signalPassFailure();
return;
}
}
// Write tensor data to flash image file
else if (failed(utils::writeFlashImageToFile(flashImageFilenameOption,
tensorsVec))) {
else if (failed(
utils::writeWeightsToFile(weightsFilenameOption, tensorsVec))) {
f.emitError("Failed to write flash image!");
signalPassFailure();
return;
}
}
} // namespace

// Creates an instance of the WriteFlashImage pass.
std::unique_ptr<OperationPass<func::FuncOp>> createWriteFlashImagePass() {
return std::make_unique<WriteFlashImage>();
// Creates an instance of the WriteWeights pass.
std::unique_ptr<OperationPass<func::FuncOp>> createWriteWeightsPass() {
return std::make_unique<WriteWeights>();
}

static PassRegistration<WriteFlashImage> pass;
static PassRegistration<WriteWeights> pass;

} // namespace xcore
} // namespace mlir
Loading

0 comments on commit 0965293

Please sign in to comment.