-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Basic Comparisions * stub for icmp * pinned net onnx-mlir patch * pinned new zk-ml-dialect * ArgMin and ArgMax * hardmax moved to todo
- Loading branch information
Showing
54 changed files
with
493 additions
and
14 deletions.
There are no files selected for viewing
Submodule onnx-mlir
updated
5 files
Submodule zk-ml-dialect
updated
9 files
130 changes: 130 additions & 0 deletions
130
mlir-assigner/include/mlir-assigner/components/comparison/argminmax.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#ifndef CRYPTO3_ASSIGNER_FIXEDPOINT_ARGMINMAX_HPP | ||
#define CRYPTO3_ASSIGNER_FIXEDPOINT_ARGMINMAX_HPP | ||
|
||
#include "mlir/Dialect/zkml/IR/DotProduct.h" | ||
#include <mlir/Dialect/Arith/IR/Arith.h> | ||
|
||
#include <nil/crypto3/zk/snark/arithmetization/plonk/constraint_system.hpp> | ||
|
||
#include <nil/blueprint/component.hpp> | ||
#include <nil/blueprint/basic_non_native_policy.hpp> | ||
#include <nil/blueprint/components/algebra/fixedpoint/lookup_tables/tester.hpp> // TODO: check if there is a new mechanism for this in nil upstream | ||
|
||
#include <mlir-assigner/helper/asserts.hpp> | ||
#include <mlir-assigner/memory/stack_frame.hpp> | ||
#include <mlir-assigner/components/handle_component.hpp> | ||
|
||
namespace nil { | ||
namespace blueprint { | ||
|
||
template<typename BlueprintFieldType, typename ArithmetizationParams> | ||
void handle_argmin( | ||
mlir::zkml::ArgMinOp &operation, | ||
stack_frame<crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>> &frame, | ||
circuit_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> &bp, | ||
assignment_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> | ||
&assignment, | ||
crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type> &nextIndex, | ||
std::uint32_t start_row) { | ||
using component_type = components::fix_argmin< | ||
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>, | ||
BlueprintFieldType, basic_non_native_policy<BlueprintFieldType>>; | ||
|
||
using input_type = typename component_type::input_type; | ||
auto acc = frame.locals.find(mlir::hash_value(operation.getAcc())); | ||
auto next = frame.locals.find(mlir::hash_value(operation.getNext())); | ||
auto accIndex = frame.locals.find(mlir::hash_value(operation.getAccIndex())); | ||
ASSERT(acc != frame.locals.end()); | ||
ASSERT(next != frame.locals.end()); | ||
ASSERT(accIndex != frame.locals.end()); | ||
input_type instance_input; | ||
instance_input.x = acc->second; | ||
instance_input.y = next->second; | ||
instance_input.index_x = accIndex->second; | ||
|
||
using manifest_reader = detail::ManifestReader<component_type, ArithmetizationParams, 1, 1>; | ||
const auto p = detail::PolicyManager::get_parameters( | ||
detail::ManifestReader<component_type, ArithmetizationParams, 1, 1>::get_witness(0, 1, 1)); | ||
component_type component(p.witness, manifest_reader::get_constants(), manifest_reader::get_public_inputs(), | ||
1, 1, var_value(assignment, nextIndex), operation.getSelectLastIndex()); | ||
|
||
if constexpr (nil::blueprint::use_custom_lookup_tables<component_type>()) { | ||
auto lookup_tables = component.component_custom_lookup_tables(); | ||
for (auto &t : lookup_tables) { | ||
bp.register_lookup_table( | ||
std::shared_ptr<nil::crypto3::zk::snark::lookup_table_definition<BlueprintFieldType>>(t)); | ||
} | ||
}; | ||
|
||
if constexpr (nil::blueprint::use_lookups<component_type>()) { | ||
auto lookup_tables = component.component_lookup_tables(); | ||
for (auto &[k, v] : lookup_tables) { | ||
bp.reserve_table(k); | ||
} | ||
}; | ||
|
||
handle_component_input<BlueprintFieldType, ArithmetizationParams, component_type>(assignment, instance_input); | ||
|
||
components::generate_circuit(component, bp, assignment, instance_input, start_row); | ||
auto result = components::generate_assignments(component, assignment, instance_input, start_row); | ||
frame.locals[mlir::hash_value(operation.getResult(0))] = result.min; | ||
frame.locals[mlir::hash_value(operation.getResult(1))] = result.index; | ||
} | ||
|
||
template<typename BlueprintFieldType, typename ArithmetizationParams> | ||
void handle_argmax( | ||
mlir::zkml::ArgMaxOp &operation, | ||
stack_frame<crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type>> &frame, | ||
circuit_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> &bp, | ||
assignment_proxy<crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>> | ||
&assignment, | ||
crypto3::zk::snark::plonk_variable<typename BlueprintFieldType::value_type> &nextIndex, | ||
std::uint32_t start_row) { | ||
using component_type = components::fix_argmax< | ||
crypto3::zk::snark::plonk_constraint_system<BlueprintFieldType, ArithmetizationParams>, | ||
BlueprintFieldType, basic_non_native_policy<BlueprintFieldType>>; | ||
|
||
using input_type = typename component_type::input_type; | ||
auto acc = frame.locals.find(mlir::hash_value(operation.getAcc())); | ||
auto next = frame.locals.find(mlir::hash_value(operation.getNext())); | ||
auto accIndex = frame.locals.find(mlir::hash_value(operation.getAccIndex())); | ||
ASSERT(acc != frame.locals.end()); | ||
ASSERT(next != frame.locals.end()); | ||
ASSERT(accIndex != frame.locals.end()); | ||
input_type instance_input; | ||
instance_input.x = acc->second; | ||
instance_input.y = next->second; | ||
instance_input.index_x = accIndex->second; | ||
|
||
using manifest_reader = detail::ManifestReader<component_type, ArithmetizationParams, 1, 1>; | ||
const auto p = detail::PolicyManager::get_parameters( | ||
detail::ManifestReader<component_type, ArithmetizationParams, 1, 1>::get_witness(0, 1, 1)); | ||
component_type component(p.witness, manifest_reader::get_constants(), manifest_reader::get_public_inputs(), | ||
1, 1, var_value(assignment, nextIndex), operation.getSelectLastIndex()); | ||
|
||
if constexpr (nil::blueprint::use_custom_lookup_tables<component_type>()) { | ||
auto lookup_tables = component.component_custom_lookup_tables(); | ||
for (auto &t : lookup_tables) { | ||
bp.register_lookup_table( | ||
std::shared_ptr<nil::crypto3::zk::snark::lookup_table_definition<BlueprintFieldType>>(t)); | ||
} | ||
}; | ||
|
||
if constexpr (nil::blueprint::use_lookups<component_type>()) { | ||
auto lookup_tables = component.component_lookup_tables(); | ||
for (auto &[k, v] : lookup_tables) { | ||
bp.reserve_table(k); | ||
} | ||
}; | ||
|
||
handle_component_input<BlueprintFieldType, ArithmetizationParams, component_type>(assignment, instance_input); | ||
|
||
components::generate_circuit(component, bp, assignment, instance_input, start_row); | ||
auto result = components::generate_assignments(component, assignment, instance_input, start_row); | ||
frame.locals[mlir::hash_value(operation.getResult(0))] = result.max; | ||
frame.locals[mlir::hash_value(operation.getResult(1))] = result.index; | ||
} | ||
} // namespace blueprint | ||
} // namespace nil | ||
|
||
#endif // CRYPTO3_ASSIGNER_FIXEDPOINT_ARGMINMAX_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**.mlir |
1 change: 1 addition & 0 deletions
1
mlir-assigner/tests/Ops/NeedsCompilerWork/Hardmax/HardMaxAxis.json
Large diffs are not rendered by default.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
mlir-assigner/tests/Ops/NeedsCompilerWork/Hardmax/HardMaxAxis.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module attributes {llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", llvm.target_triple = "x86_64-pc-linux-gnu", "onnx-mlir.symbol-postfix" = "hardmaxaxis.mlir"} { | ||
func.func @main_graph(%arg0: memref<1x10x20x30xf32>) -> memref<1x10x20x30xf32> attributes {input_names = ["in_a"], llvm.emit_c_interface, output_names = ["out_a"]} { | ||
%cst = arith.constant 0.000000e+00 : f32 | ||
%cst_0 = arith.constant 1.000000e+00 : f32 | ||
%c0 = arith.constant 0 : index | ||
%alloc = memref.alloc() {alignment = 16 : i64} : memref<1x10x20x30xf32> | ||
%alloc_1 = memref.alloc() {alignment = 16 : i64} : memref<1x10x20x30xindex> | ||
affine.for %arg1 = 0 to 1 { | ||
affine.for %arg2 = 0 to 10 { | ||
affine.for %arg3 = 0 to 20 { | ||
affine.for %arg4 = 0 to 30 { | ||
affine.store %c0, %alloc_1[%arg1, %arg2, %arg3, %arg4] : memref<1x10x20x30xindex> | ||
} | ||
} | ||
} | ||
} | ||
affine.for %arg1 = 0 to 1 { | ||
affine.for %arg2 = 0 to 10 { | ||
affine.for %arg3 = 0 to 20 { | ||
affine.for %arg4 = 0 to 30 { | ||
%0 = affine.load %alloc_1[%c0, %arg2, %arg3, %arg4] : memref<1x10x20x30xindex> | ||
%1 = memref.load %arg0[%0, %arg2, %arg3, %arg4] : memref<1x10x20x30xf32> | ||
%2 = affine.load %arg0[%arg1, %arg2, %arg3, %arg4] : memref<1x10x20x30xf32> | ||
%3 = arith.cmpf ogt, %2, %1 : f32 | ||
scf.if %3 { | ||
affine.store %arg1, %alloc_1[%c0, %arg2, %arg3, %arg4] : memref<1x10x20x30xindex> | ||
} | ||
} | ||
} | ||
} | ||
} | ||
affine.for %arg1 = 0 to 1 { | ||
affine.for %arg2 = 0 to 10 { | ||
affine.for %arg3 = 0 to 20 { | ||
affine.for %arg4 = 0 to 30 { | ||
%0 = affine.load %alloc_1[%c0, %arg2, %arg3, %arg4] : memref<1x10x20x30xindex> | ||
%1 = arith.cmpi eq, %0, %arg1 : index | ||
scf.if %1 { | ||
affine.store %cst_0, %alloc[%arg1, %arg2, %arg3, %arg4] : memref<1x10x20x30xf32> | ||
} else { | ||
affine.store %cst, %alloc[%arg1, %arg2, %arg3, %arg4] : memref<1x10x20x30xf32> | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return %alloc : memref<1x10x20x30xf32> | ||
} | ||
"krnl.entry_point"() {func = @main_graph, numInputs = 1 : i32, numOutputs = 1 : i32, signature = "[ { \22type\22 : \22f32\22 , \22dims\22 : [1 , 10 , 20 , 30] , \22name\22 : \22in_a\22 }\0A\0A]\00@[ { \22type\22 : \22f32\22 , \22dims\22 : [1 , 10 , 20 , 30] , \22name\22 : \22out_a\22 }\0A\0A]\00"} : () -> () | ||
} |
Binary file not shown.
3 changes: 3 additions & 0 deletions
3
mlir-assigner/tests/Ops/NeedsCompilerWork/Hardmax/HardMaxAxis.res
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
mlir-assigner/tests/Ops/NeedsCompilerWork/Hardmax/HardMaxSimple.json
Large diffs are not rendered by default.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
mlir-assigner/tests/Ops/NeedsCompilerWork/Hardmax/HardMaxSimple.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module attributes {llvm.data_layout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128", llvm.target_triple = "x86_64-pc-linux-gnu", "onnx-mlir.symbol-postfix" = "hardmaxsimple.mlir"} { | ||
func.func @main_graph(%arg0: memref<1x10x20x30xf32>) -> memref<1x10x20x30xf32> attributes {input_names = ["in_a"], llvm.emit_c_interface, output_names = ["out_a"]} { | ||
%cst = arith.constant 0.000000e+00 : f32 | ||
%cst_0 = arith.constant 1.000000e+00 : f32 | ||
%c0 = arith.constant 0 : index | ||
%alloc = memref.alloc() {alignment = 16 : i64} : memref<1x10x20x30xf32> | ||
%alloc_1 = memref.alloc() {alignment = 16 : i64} : memref<1x10x20x1xindex> | ||
affine.for %arg1 = 0 to 1 { | ||
affine.for %arg2 = 0 to 10 { | ||
affine.for %arg3 = 0 to 20 { | ||
affine.for %arg4 = 0 to 1 { | ||
affine.store %c0, %alloc_1[%arg1, %arg2, %arg3, %arg4] : memref<1x10x20x1xindex> | ||
} | ||
} | ||
} | ||
} | ||
affine.for %arg1 = 0 to 1 { | ||
affine.for %arg2 = 0 to 10 { | ||
affine.for %arg3 = 0 to 20 { | ||
affine.for %arg4 = 0 to 30 { | ||
%0 = affine.load %alloc_1[%arg1, %arg2, %arg3, %c0] : memref<1x10x20x1xindex> | ||
%1 = memref.load %arg0[%arg1, %arg2, %arg3, %0] : memref<1x10x20x30xf32> | ||
%2 = affine.load %arg0[%arg1, %arg2, %arg3, %arg4] : memref<1x10x20x30xf32> | ||
%3 = arith.cmpf ogt, %2, %1 : f32 | ||
scf.if %3 { | ||
affine.store %arg4, %alloc_1[%arg1, %arg2, %arg3, %c0] : memref<1x10x20x1xindex> | ||
} | ||
} | ||
} | ||
} | ||
} | ||
affine.for %arg1 = 0 to 1 { | ||
affine.for %arg2 = 0 to 10 { | ||
affine.for %arg3 = 0 to 20 { | ||
affine.for %arg4 = 0 to 30 { | ||
%0 = affine.load %alloc_1[%arg1, %arg2, %arg3, %c0] : memref<1x10x20x1xindex> | ||
%1 = arith.cmpi eq, %0, %arg4 : index | ||
scf.if %1 { | ||
affine.store %cst_0, %alloc[%arg1, %arg2, %arg3, %arg4] : memref<1x10x20x30xf32> | ||
} else { | ||
affine.store %cst, %alloc[%arg1, %arg2, %arg3, %arg4] : memref<1x10x20x30xf32> | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return %alloc : memref<1x10x20x30xf32> | ||
} | ||
"krnl.entry_point"() {func = @main_graph, numInputs = 1 : i32, numOutputs = 1 : i32, signature = "[ { \22type\22 : \22f32\22 , \22dims\22 : [1 , 10 , 20 , 30] , \22name\22 : \22in_a\22 }\0A\0A]\00@[ { \22type\22 : \22f32\22 , \22dims\22 : [1 , 10 , 20 , 30] , \22name\22 : \22out_a\22 }\0A\0A]\00"} : () -> () | ||
} |
17 changes: 17 additions & 0 deletions
17
mlir-assigner/tests/Ops/NeedsCompilerWork/Hardmax/HardMaxSimple.onnx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
:h | ||
in_aout_a"HardmaxHardMaxSimpleZ | ||
in_a | ||
|
||
b | ||
out_a | ||
|
||
B | ||
|
3 changes: 3 additions & 0 deletions
3
mlir-assigner/tests/Ops/NeedsCompilerWork/Hardmax/HardMaxSimple.res
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
**.mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"memref": {"data": [0.0930633544921875, 0.1421966552734375, 0.0830841064453125, 0.876251220703125, 0.2035980224609375, 0.38433837890625, 0.6446533203125, 0.8219451904296875, 0.4010162353515625, 0.7171173095703125], "dims": [1, 10], "type": "f32"}}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
:c | ||
" | ||
in_aout_a"ArgMax* | ||
axis�ArgMaxSimpleZ | ||
in_a | ||
b | ||
out_a | ||
B |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Result: | ||
memref<1x1xi64>[3] | ||
23 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"memref": {"data": [0.2682342529296875, 0.8263092041015625, 0.6944732666015625, 0.6793212890625, 0.09832763671875, 0.728607177734375, 0.28619384765625, 0.761749267578125, 0.6746368408203125, 0.40509033203125], "dims": [1, 10], "type": "f32"}}] |
Binary file not shown.
Oops, something went wrong.