Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling the measurements associated with multiple runs #2547

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/onnx-mlir/Compiler/OMCompilerRuntimeTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ typedef enum {
InstrumentBeforeOp = 0x0,
InstrumentAfterOp = 0x1,
InstrumentReportTime = 0x2,
InstrumentReportMemory = 0x4,
InstrumentReportMemory = 0x3,
InstrumentInit = 0x4,
} InstrumentActions;

/* Definition of setter/getter from a 64 bit unsigned int. Use 64 bit only to
Expand Down Expand Up @@ -67,6 +68,11 @@ typedef enum {
#define IS_INSTRUMENT_REPORT_MEMORY(x) \
((x) & (0x1ull << (unsigned int)InstrumentReportMemory))

#define SET_INSTRUMENT_INIT(x) \
(x) = (x) | (0x1ull << (unsigned int)InstrumentInit)
#define IS_INSTRUMENT_INIT(x) \
((x) & (0x1ull << (unsigned int)InstrumentInit))

/* Second - third byte. */
#define INSTRUMENT_OP_NAME_MASK 0x3Full /* Max 64 chars. */
#define SET_INSTRUMENT_OP_NAME_LEN(x, len) \
Expand Down
4 changes: 2 additions & 2 deletions src/Accelerators/Accelerator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ class Accelerator {
//===--------------------------------------------------------------------===//

/// Convert TensorType to MemRefType.
/// Acccelators may have special versions of TensorType. If not, override this
/// method and return nullptr.
/// Accelerators may have special versions of TensorType. If not, override
/// this method and return nullptr.
virtual mlir::MemRefType convertTensorTypeToMemRefType(
const mlir::TensorType tensorType) const = 0;

Expand Down
16 changes: 7 additions & 9 deletions src/Runtime/OMInstrument.inc
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ FILE *getInstrumentFile() {
fout = stdout;
if (getenv("ONNX_MLIR_INSTRUMENT_FILE")) {
char *fileName = getenv("ONNX_MLIR_INSTRUMENT_FILE");
FILE *newFileHandle = fopen(fileName, "w");
FILE *newFileHandle = fopen(fileName, "w+");
if (newFileHandle) {
fout = newFileHandle;
}
Expand Down Expand Up @@ -193,28 +193,26 @@ void OMInstrumentInit() {
if (!instrumentReportDisabled) {
TimeInit();
}

fprintf(fout, "==START-REPORT==\n");
}

void OMInstrumentPoint(const char *opName, int64_t iTag, const char *nodeName) {
if (instrumentReportDisabled)
return;

// Initialize on first call. Would prefer to call explicitly, but there is
// currently no support for that.
static bool firstTime = true;
if (firstTime) {
OMInstrumentInit();
firstTime = false;
}

// Detect which reporting we have to do here.
uint64_t tag = iTag;
bool initInstrument = IS_INSTRUMENT_INIT(tag);
bool isBefore = IS_INSTRUMENT_BEFORE_OP(tag);
bool reportTime =
!instrumentReportTimeDisabled && IS_INSTRUMENT_REPORT_TIME(tag);
bool reportMem =
!instrumentReportMemoryDisabled && IS_INSTRUMENT_REPORT_MEMORY(tag);

if (initInstrument)
OMInstrumentInit();

if (!reportTime && !reportMem) {
fprintf(fout, "==TICK-REPORT==, %i\n", instrumentCounter++);
return;
Expand Down
18 changes: 15 additions & 3 deletions src/Transform/ONNX/InstrumentPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class InstrumentPass
if (instrumentOps == "" || instrumentOps == "NONE")
return;
init(instrumentOps);
bool hasInitializedRuntime = false;

// Iterate on the operations nested in this function
getOperation().walk([&](mlir::Operation *op) -> WalkResult {
Expand All @@ -154,13 +155,24 @@ class InstrumentPass
if (std::regex_match(opName, re)) {
Location loc = op->getLoc();
OpBuilder opBuilder(op);
if (instrumentBefore)
opBuilder.create<mlir::KrnlInstrumentOp>(loc, op, beforeTag());
if (instrumentBefore) {
uint64_t tag = beforeTag();
if (!hasInitializedRuntime) {
SET_INSTRUMENT_INIT(tag);
hasInitializedRuntime = true;
}
opBuilder.create<mlir::KrnlInstrumentOp>(loc, op, tag);
}

// Can not insert after Op (e.g. ONNXYieldOP) with IsTerminator Trait
if (instrumentAfter && !op->hasTrait<OpTrait::IsTerminator>()) {
opBuilder.setInsertionPointAfter(op);
opBuilder.create<mlir::KrnlInstrumentOp>(loc, op, afterTag());
uint64_t tag = afterTag();
if (!hasInitializedRuntime) {
SET_INSTRUMENT_INIT(tag);
hasInitializedRuntime = true;
}
opBuilder.create<mlir::KrnlInstrumentOp>(loc, op, tag);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// RUN: onnx-mlir --mcpu=z16 --maccel=NNPA --printIR --EmitZHighIR --instrument-stage=Onnx --instrument-ops=onnx.* --InstrumentBeforeOp --InstrumentAfterOp --InstrumentReportTime -tag="test" %s | FileCheck %s
// RUN: onnx-mlir --mcpu=z16 --maccel=NNPA --printIR --EmitZHighIR -profile-ir=Onnx %s | FileCheck %s

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating this!

// -----

func.func @test_instrument_add_onnx(%arg0 : tensor<10x10xf32>, %arg1 : tensor<10x10xf32>) -> tensor<*xf32> {
%0 = "onnx.Add"(%arg0, %arg1) {onnx_node_name = "onnx.Add"} : (tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<*xf32>
"onnx.Return"(%0) : (tensor<*xf32>) -> ()
}

// CHECK-LABEL: func.func @test_instrument_add_onnx
// CHECK: "krnl.runtime_instrument"() {nodeName = "onnx.Add", opName = "onnx.Add", tag = 5 : i64} : () -> ()
// CHECK: "krnl.runtime_instrument"() {nodeName = "onnx.Add", opName = "onnx.Add", tag = 21 : i64} : () -> ()
// CHECK: "zhigh.Stick"
// CHECK: "zhigh.Stick"
// CHECK: "zhigh.Add"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// RUN: onnx-mlir --mcpu=z16 --maccel=NNPA --printIR --EmitZHighIR --instrument-stage=ZHigh --instrument-ops="onnx.*,zhigh.*" --InstrumentBeforeOp --InstrumentAfterOp --InstrumentReportTime -tag="test" %s | FileCheck %s
// RUN: onnx-mlir --mcpu=z16 --maccel=NNPA --printIR --EmitZHighIR --profile-ir=ZHigh %s | FileCheck %s

// -----

func.func @test_instrument_add_onnx_zhigh(%arg0 : tensor<10x10xf32>, %arg1 : tensor<10x1xf32>) -> tensor<*xf32> {
%0 = "onnx.Add"(%arg0, %arg1) {onnx_node_name = "onnx.Add1"} : (tensor<10x10xf32>, tensor<10x1xf32>) -> tensor<*xf32>
Expand All @@ -7,7 +9,7 @@ func.func @test_instrument_add_onnx_zhigh(%arg0 : tensor<10x10xf32>, %arg1 : ten
}

// CHECK-LABEL: func.func @test_instrument_add_onnx_zhigh
// CHECK: "krnl.runtime_instrument"() {nodeName = "onnx.Add1", opName = "onnx.Add", tag = 5 : i64} : () -> ()
// CHECK: "krnl.runtime_instrument"() {nodeName = "onnx.Add1", opName = "onnx.Add", tag = 21 : i64} : () -> ()
// CHECK: "onnx.Add"
// CHECK: "krnl.runtime_instrument"() {nodeName = "onnx.Add1", opName = "onnx.Add", tag = 6 : i64} : () -> ()
// CHECK: "krnl.runtime_instrument"() {opName = "zhigh.Stick", tag = 5 : i64} : () -> ()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// RUN: onnx-mlir --mcpu=z16 --maccel=NNPA --printIR --EmitZLowIR --instrument-stage=ZHigh --instrument-ops="zhigh.*" --InstrumentBeforeOp --InstrumentAfterOp --InstrumentReportTime -tag="test" %s | FileCheck %s
// RUN: onnx-mlir --mcpu=z16 --maccel=NNPA --printIR --EmitZLowIR --instrument-stage=ZHigh --instrument-ops=zhigh.* --InstrumentBeforeOp --InstrumentAfterOp --InstrumentReportTime %s | FileCheck %s

// -----

func.func @test_instrument_add_zhigh(%arg0 : tensor<10x10xf32>, %arg1 : tensor<10x10xf32>) -> tensor<*xf32> {
%0 = "onnx.Add"(%arg0, %arg1) : (tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<*xf32>
"onnx.Return"(%0) : (tensor<*xf32>) -> ()
}

// CHECK-LABEL: func.func @test_instrument_add_zhigh
// CHECK: "krnl.runtime_instrument"() {opName = "zhigh.Stick", tag = 5 : i64} : () -> ()
// CHECK: "krnl.runtime_instrument"() {opName = "zhigh.Stick", tag = 21 : i64} : () -> ()
// CHECK: memref.alloc()
// CHECK: "zlow.stick"
// CHECK: "krnl.runtime_instrument"() {opName = "zhigh.Stick", tag = 6 : i64} : () -> ()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// RUN: onnx-mlir --mcpu=z16 --maccel=NNPA --printIR --EmitZLowIR --instrument-stage=ZLow --instrument-ops="zlow.*" --InstrumentBeforeOp --InstrumentAfterOp --InstrumentReportTime -tag="test" %s | FileCheck %s
// RUN: onnx-mlir --mcpu=z16 --maccel=NNPA --printIR --EmitZLowIR --instrument-stage=ZLow --instrument-ops=zlow.* --InstrumentBeforeOp --InstrumentAfterOp --InstrumentReportTime %s | FileCheck %s

// -----

func.func @test_instrument_add_zlow(%arg0 : tensor<10x10xf32>, %arg1 : tensor<10x10xf32>) -> tensor<*xf32> {
%0 = "onnx.Add"(%arg0, %arg1) : (tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<*xf32>
"onnx.Return"(%0) : (tensor<*xf32>) -> ()
}

// CHECK-LABEL: func.func @test_instrument_add_zlow
// CHECK: "krnl.runtime_instrument"() {opName = "zlow.stick", tag = 5 : i64} : () -> ()
// CHECK: "krnl.runtime_instrument"() {opName = "zlow.stick", tag = 21 : i64} : () -> ()
// CHECK: "zlow.stick"
// CHECK: "krnl.runtime_instrument"() {opName = "zlow.stick", tag = 6 : i64} : () -> ()
// CHECK: memref.alloc()
Expand All @@ -26,5 +28,3 @@ func.func @test_instrument_add_zlow(%arg0 : tensor<10x10xf32>, %arg1 : tensor<10
// CHECK: "krnl.runtime_instrument"() {opName = "zlow.unstick", tag = 6 : i64} : () -> ()
// CHECK: return
// CHECK: }

// -----
5 changes: 3 additions & 2 deletions test/mlir/conversion/instrument/add.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ func.func @test_instrument_add_onnx(%arg0 : tensor<10x10xf32>, %arg1 : tensor<10
"onnx.Return"(%0) : (tensor<*xf32>) -> ()
}

// -----

// CHECK-LABEL: func.func @test_instrument_add_onnx
// CHECK: "krnl.runtime_instrument"() {nodeName = "model/add1", opName = "onnx.Add", tag = 5 : i64} : () -> ()
// CHECK: "krnl.runtime_instrument"() {nodeName = "model/add1", opName = "onnx.Add", tag = 21 : i64} : () -> ()
// CHECK: [[RES_:%.+]] = memref.alloc()
// CHECK: affine.for [[I_0_:%.+]] = 0 to 10 {
// CHECK: affine.for [[I_1_:%.+]] = 0 to 10 {
Expand All @@ -20,4 +22,3 @@ func.func @test_instrument_add_onnx(%arg0 : tensor<10x10xf32>, %arg1 : tensor<10
// CHECK: return
// CHECK: }

// -----
5 changes: 3 additions & 2 deletions test/mlir/conversion/instrument/onnx_add.mlir
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// RUN: onnx-mlir --printIR --EmitMLIR --instrument-ops=onnx.Add --InstrumentBeforeOp --InstrumentAfterOp --InstrumentReportTime %s | FileCheck %s

// -----

func.func @test_instrument_add_onnx(%arg0 : tensor<10x10xf32>, %arg1 : tensor<10x10xf32>) -> tensor<*xf32> {
%0 = "onnx.Add"(%arg0, %arg1) {onnx_node_name = "model/add1"} : (tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<*xf32>
"onnx.Return"(%0) : (tensor<*xf32>) -> ()
}

// CHECK-LABEL: func.func @test_instrument_add_onnx
// CHECK: "krnl.runtime_instrument"() {nodeName = "model/add1", opName = "onnx.Add", tag = 5 : i64} : () -> ()
// CHECK: "krnl.runtime_instrument"() {nodeName = "model/add1", opName = "onnx.Add", tag = 21 : i64} : () -> ()
// CHECK: [[RES_:%.+]] = memref.alloc()
// CHECK: affine.for [[I_0_:%.+]] = 0 to 10 {
// CHECK: affine.for [[I_1_:%.+]] = 0 to 10 {
Expand All @@ -20,4 +22,3 @@ func.func @test_instrument_add_onnx(%arg0 : tensor<10x10xf32>, %arg1 : tensor<10
// CHECK: return
// CHECK: }

// -----
6 changes: 3 additions & 3 deletions test/mlir/conversion/instrument/onnx_all.mlir
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// RUN: onnx-mlir --printIR --EmitMLIR --instrument-ops=onnx.* --InstrumentBeforeOp --InstrumentAfterOp --InstrumentReportTime %s | FileCheck %s

// -----

func.func @test_instrument_add_onnx(%arg0 : tensor<10x10xf32>, %arg1 : tensor<10x10xf32>) -> tensor<*xf32> {
%0 = "onnx.Add"(%arg0, %arg1) {onnx_node_name = "model/add1"} : (tensor<10x10xf32>, tensor<10x10xf32>) -> tensor<*xf32>
"onnx.Return"(%0) : (tensor<*xf32>) -> ()
}

// CHECK-LABEL: func.func @test_instrument_add_onnx
// CHECK: "krnl.runtime_instrument"() {nodeName = "model/add1", opName = "onnx.Add", tag = 5 : i64} : () -> ()
// CHECK: "krnl.runtime_instrument"() {nodeName = "model/add1", opName = "onnx.Add", tag = 21 : i64} : () -> ()
// CHECK: [[RES_:%.+]] = memref.alloc()
// CHECK: affine.for [[I_0_:%.+]] = 0 to 10 {
// CHECK: affine.for [[I_1_:%.+]] = 0 to 10 {
Expand All @@ -19,5 +21,3 @@ func.func @test_instrument_add_onnx(%arg0 : tensor<10x10xf32>, %arg1 : tensor<10
// CHECK: "krnl.runtime_instrument"() {nodeName = "model/add1", opName = "onnx.Add", tag = 6 : i64} : () -> ()
// CHECK: return
// CHECK: }

// -----
3 changes: 3 additions & 0 deletions utils/RunONNXModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,9 @@ def warning(msg):
def data_without_top_bottom_quartile(data, percent):
data = np.array(sorted(data))
trim = int(percent * data.size / 100.0)
if trim == 0 or data.size - 2 * trim < 1:
# Want at least one element, return as is.
return data
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I sometimes got the error here with a small number of iterations, say 3.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So did I :-)

return data[trim:-trim]


Expand Down
Loading
Loading