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

[DAPHNE-#854] Add Passes for Matrix Result Property Recording and Insertion in IR #855

Draft
wants to merge 72 commits into
base: main
Choose a base branch
from

Conversation

StoeckOverflow
Copy link
Contributor

Attention: This is a Draft PR and is still WIP

This Draft PR introduces two new passes: RecordPropertiesPass and InsertPropertiesPass. These passes enable the recording of runtime properties of matrix result objects and the reinsertion of these properties into the IR (Intermediate Representation) during subsequent runs. This process aims to improve optimization and analysis.

Changes:

  • Property Recording Pass:

    • Records runtime properties (until now only sparsity) of matrix results during IR transformations.
    • Leverages a new PropertyLogger to capture and save properties in a JSON file.
  • Property Insertion Pass:

    • Reads properties from the JSON file and reinserts them into IR operations, allowing reuse of runtime data.
    • Supports modifying matrix types in the IR based on the previously recorded properties.

Features:

  • New Configuration Options:
    • enable_property_recording: Enables the recording of matrix properties.
    • enable_property_insert: Enables reinserting properties from a previous run.
    • properties_file_path: Specifies the path to the JSON file for storing or retrieving matrix properties.

Documentation:

  • Updated RunningDaphneLocally.md to reflect the new configuration options and command-line flags.

Testing:

  • Added tests for recording matrix properties inside loops and conditional blocks (scf.for, scf.if, scf.while).
  • Verified property recording for both dense and sparse matrix types.

Problems:

  • Issue with property insertion testing: The test for inserting properties in code generation does not work correctly at the moment. The original intention was to invoke the property insertion pass as follows:
// RUN: daphne-opt --insert-properties="<path to properties.json>" %s | FileCheck --check-prefix=CHECK-INSERTED
...

This approach did not work as expected, and further work is required to fix the issue.

  • Loop Argument Sparsity Issue:
    I encountered a specific issue where the property insertion pass fails under certain conditions. Here’s an example Daphne script that triggers the error:

    Daphne Script:

    X = rand(10, 10, 0, 10, 0.29, 42);
    Y = rand(10, 10, 0, 10, 0.29, 42);
    
    Z = X + Y + 1e-6;
    P = (X + Y)*2 + 1e-6;
    k = 10;
    
    for (i in 1:10) {
        Z = Z + log(Z, k);
        P = P + log(P, k);
    }
    
    print(sum(Z));
    print(sum(P));

Error Output:

bin/daphne --enable_property_insert --select-matrix-repr propRecord_for_test.daphne
loc("propRecord_for_test.daphne":8:0): error: 'scf.for' op  along control flow edge from parent operands to Region #0: source type #0 '!daphne.Matrix<10x10xf64:sp[1.000000e+00]>' should match input type #0 '!daphne.Matrix<10x10xf64>'
"builtin.module"() ({
  "func.func"() ({
    %0 = "daphne.constant"() {value = 1 : index} : () -> index
    %1 = "daphne.constant"() {value = 11 : index} : () -> index
    %2 = "daphne.constant"() {value = 2 : si64} : () -> si64
    %3 = "daphne.constant"() {value = 10 : index} : () -> index
    %4 = "daphne.constant"() {value = false} : () -> i1
    %5 = "daphne.constant"() {value = true} : () -> i1
    %6 = "daphne.constant"() {value = 9.9999999999999995E-7 : f64} : () -> f64
    %7 = "daphne.constant"() {value = 10 : si64} : () -> si64
    %8 = "daphne.constant"() {value = 0 : si64} : () -> si64
    %9 = "daphne.constant"() {value = 2.900000e-01 : f64} : () -> f64
    %10 = "daphne.constant"() {value = 42 : si64} : () -> si64
    %11 = "daphne.randMatrix"(%3, %3, %8, %7, %9, %10) : (index, index, si64, si64, f64, si64) -> !daphne.Matrix<10x10xsi64:sp[2.900000e-01]>
    %12 = "daphne.randMatrix"(%3, %3, %8, %7, %9, %10) : (index, index, si64, si64, f64, si64) -> !daphne.Matrix<10x10xsi64:sp[2.900000e-01]>
    %13 = "daphne.ewAdd"(%11, %12) : (!daphne.Matrix<10x10xsi64:sp[2.900000e-01]>, !daphne.Matrix<10x10xsi64:sp[2.900000e-01]>) -> !daphne.Matrix<10x10xsi64:sp[2.900000e-01]>
    %14 = "daphne.ewAdd"(%13, %6) : (!daphne.Matrix<10x10xsi64:sp[2.900000e-01]>, f64) -> !daphne.Matrix<10x10xf64:sp[1.000000e+00]>
    %15 = "daphne.ewMul"(%13, %2) : (!daphne.Matrix<10x10xsi64:sp[2.900000e-01]>, si64) -> !daphne.Matrix<10x10xsi64:sp[2.900000e-01]>
    %16 = "daphne.ewAdd"(%15, %6) : (!daphne.Matrix<10x10xsi64:sp[2.900000e-01]>, f64) -> !daphne.Matrix<10x10xf64:sp[1.000000e+00]>
    %17:2 = "scf.for"(%0, %1, %0, %14, %16) ({
    ^bb0(%arg0: index, %arg1: !daphne.Matrix<10x10xf64>, %arg2: !daphne.Matrix<10x10xf64>):
      %20 = "daphne.cast"(%arg0) : (index) -> si64
      %21 = "daphne.ewLog"(%arg1, %7) : (!daphne.Matrix<10x10xf64>, si64) -> !daphne.Matrix<10x10xf64>
      %22 = "daphne.ewAdd"(%arg1, %21) : (!daphne.Matrix<10x10xf64>, !daphne.Matrix<10x10xf64>) -> !daphne.Matrix<10x10xf64>
      %23 = "daphne.ewLog"(%arg2, %7) : (!daphne.Matrix<10x10xf64>, si64) -> !daphne.Matrix<10x10xf64>
      %24 = "daphne.ewAdd"(%arg2, %23) : (!daphne.Matrix<10x10xf64>, !daphne.Matrix<10x10xf64>) -> !daphne.Matrix<10x10xf64>
      "scf.yield"(%22, %24) : (!daphne.Matrix<10x10xf64>, !daphne.Matrix<10x10xf64>) -> ()
    }) : (index, index, index, !daphne.Matrix<10x10xf64:sp[1.000000e+00]>, !daphne.Matrix<10x10xf64:sp[1.000000e+00]>) -> (!daphne.Matrix<10x10xf64:sp[1.000000e+00]>, !daphne.Matrix<10x10xf64:sp[1.000000e+00]>)
    %18 = "daphne.sumAll"(%17#0) : (!daphne.Matrix<10x10xf64:sp[1.000000e+00]>) -> f64
    "daphne.print"(%18, %5, %4) : (f64, i1, i1) -> ()
    %19 = "daphne.sumAll"(%17#1) : (!daphne.Matrix<10x10xf64:sp[1.000000e+00]>) -> f64
    "daphne.print"(%19, %5, %4) : (f64, i1, i1) -> ()
    "daphne.return"() : () -> ()
  }) {function_type = () -> (), sym_name = "main"} : () -> ()
}) : () -> ()
loc("propRecord_for_test.daphne":0:0): error: module pass error

The issue is still under investigation, and further debugging is required to find a proper solution.

StoeckOverflow and others added 30 commits August 29, 2024 11:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant