eerie 0.3.3

Rustic binding to the IREE Compiler/Runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#![cfg(feature = "runtime")]

use eerie::runtime::{
    self,
    error::RuntimeError,
    hal::{Buffer, BufferMapping, BufferParams, BufferView, ElementType, Encoding, Tensor},
    vm::{FunctionLinkage, List, ToRef, ToValue, Undefined, Value},
};
use half::f16;
use log::info;
use test_log::test;

fn local_sync_device() -> (
    runtime::hal::DriverRegistry,
    runtime::hal::Driver,
    runtime::hal::Device,
) {
    let registry = runtime::hal::DriverRegistry::with_available_drivers().unwrap();
    let driver = registry.create_driver("local-sync").unwrap();
    let device = driver.create_default_device().unwrap();
    (registry, driver, device)
}

#[test]
fn test_instance() {
    runtime::vm::Instance::new().unwrap();
}

#[test]
fn test_context_with_hal_module() {
    let instance = runtime::vm::Instance::new().unwrap();
    let (_registry, _driver, device) = local_sync_device();
    let hal_module = runtime::vm::Module::hal(&instance, &device).unwrap();
    runtime::vm::Context::with_modules(&instance, &[&hal_module]).unwrap();
}

#[test]
fn device_metadata() {
    let (_registry, driver, device) = local_sync_device();
    let devices = driver.available_devices().unwrap();
    assert!(!devices.is_empty());
    assert_eq!(devices[0].ordinal, 0);
    assert!(!devices[0].name.is_empty() || !devices[0].path.is_empty());
    assert!(!device.id().is_empty());
    device.capabilities().unwrap();
    assert!(device
        .query_i64("eerie.missing.query.category", "missing-key")
        .is_err());
    device.trim().unwrap();
}

#[test]
fn dynamic_list() {
    let instance = runtime::vm::Instance::new().unwrap();
    let mut list = List::<Value<i32>>::new(4, &instance).unwrap();
    list.push_value(1.to_value()).unwrap();
    list.push_value(2.to_value()).unwrap();
    list.push_value(3.to_value()).unwrap();
    list.push_value(4.to_value()).unwrap();
    let val = list.get_value::<i32>(0).unwrap();
    drop(list);
    assert_eq!(val.get(), 1);
}

#[test]
fn ref_list() {
    let instance = runtime::vm::Instance::new().unwrap();
    let (_registry, _driver, device) = local_sync_device();
    let mut list = List::<Undefined>::new(4, &instance).unwrap();
    let buffer = BufferView::<f32>::from_host(
        &device,
        &[2, 2],
        Encoding::DenseRowMajor,
        &[1.0, 2.0, 3.0, 4.0],
    )
    .unwrap();
    info!("buffer: {:?}", buffer);
    let buffer_ref = buffer.to_ref(&instance).unwrap();
    list.push_ref(&buffer_ref).unwrap();
    list.push_ref(&buffer_ref).unwrap();
    let buffer_ref_2 = list.get_ref::<BufferView<f32>>(0).unwrap();
    let buffer_2 = buffer_ref_2.to_buffer_view().unwrap();
    info!("buffer_ref_2: {:?}", buffer_2);

    let mapping = BufferMapping::map_read(&buffer_2).unwrap();
    info!("mapping: {:?}", mapping.data());
}

#[test]
fn fp16_buffer() {
    let (_registry, _driver, device) = local_sync_device();
    let buffer = BufferView::<f16>::from_host(
        &device,
        &[2, 2],
        Encoding::DenseRowMajor,
        &[
            f16::from_f32(1.0),
            f16::from_f32(2.0),
            f16::from_f32(3.0),
            f16::from_f32(4.0),
        ],
    )
    .unwrap();

    let mapping = BufferMapping::map_read(&buffer).unwrap();
    assert_eq!(
        mapping.data(),
        &[
            f16::from_f32(1.0),
            f16::from_f32(2.0),
            f16::from_f32(3.0),
            f16::from_f32(4.0)
        ]
    );
}

#[test]
fn buffer_metadata() {
    let (_registry, _driver, device) = local_sync_device();
    let buffer = BufferView::<f32>::from_host(
        &device,
        &[2, 2],
        Encoding::DenseRowMajor,
        &[1.0, 2.0, 3.0, 4.0],
    )
    .unwrap();

    assert_eq!(buffer.rank(), 2);
    assert_eq!(buffer.shape(), vec![2, 2]);
    assert_eq!(buffer.dim(0), 2);
    assert_eq!(buffer.dim(1), 2);
    assert_eq!(buffer.element_count(), 4);
    assert_eq!(buffer.element_size(), core::mem::size_of::<f32>());
    assert_eq!(buffer.element_type(), ElementType::Float32);
    assert_eq!(buffer.encoding(), Encoding::DenseRowMajor);
}

#[test]
fn raw_buffer_allocation_and_view() {
    let (_registry, _driver, device) = local_sync_device();
    let buffer = Buffer::allocate(
        &device,
        4 * core::mem::size_of::<f32>(),
        BufferParams::default(),
    )
    .unwrap();
    assert_eq!(buffer.byte_offset(), 0);
    assert_eq!(buffer.byte_length(), 4 * core::mem::size_of::<f32>());
    assert!(buffer.allocation_size() >= buffer.byte_length());
    assert_ne!(buffer.memory_type(), 0);
    assert_ne!(buffer.allowed_access(), 0);
    assert_ne!(buffer.allowed_usage(), 0);

    let view = BufferView::<f32>::from_buffer(&buffer, &[4], Encoding::DenseRowMajor).unwrap();
    let raw_buffer = view.raw_buffer();
    assert_eq!(raw_buffer.byte_length(), buffer.byte_length());
    assert_eq!(raw_buffer.memory_type(), buffer.memory_type());
    assert_eq!(raw_buffer.allowed_access(), buffer.allowed_access());
    assert_eq!(raw_buffer.allowed_usage(), buffer.allowed_usage());

    view.write_from_slice(&device, &[1.0, 2.0, 3.0, 4.0])
        .unwrap();
    assert_eq!(view.read_to_vec(&device).unwrap(), vec![1.0, 2.0, 3.0, 4.0]);

    let subspan = buffer.subspan(0, 2 * core::mem::size_of::<f32>()).unwrap();
    assert_eq!(subspan.byte_length(), 2 * core::mem::size_of::<f32>());
}

#[test]
fn buffer_shape_mismatch_is_rejected() {
    let (_registry, _driver, device) = local_sync_device();
    let err = BufferView::<f32>::from_host(
        &device,
        &[2, 3],
        Encoding::DenseRowMajor,
        &[1.0, 2.0, 3.0, 4.0],
    )
    .unwrap_err();

    assert!(matches!(err, RuntimeError::InvalidArgument(_)));
}

#[test]
fn tensor_read_write_and_copy() {
    let (_registry, _driver, device) = local_sync_device();
    let tensor = Tensor::<f32>::from_slice(&device, &[2, 2], &[1.0, 2.0, 3.0, 4.0]).unwrap();
    assert_eq!(tensor.shape(), vec![2, 2]);
    assert_eq!(
        tensor.read_to_vec(&device).unwrap(),
        vec![1.0, 2.0, 3.0, 4.0]
    );

    tensor
        .write_from_slice(&device, &[5.0, 6.0, 7.0, 8.0])
        .unwrap();
    assert_eq!(
        tensor.read_to_vec(&device).unwrap(),
        vec![5.0, 6.0, 7.0, 8.0]
    );

    let target = Tensor::<f32>::from_slice(&device, &[2, 2], &[0.0, 0.0, 0.0, 0.0]).unwrap();
    tensor
        .as_buffer_view()
        .copy_to(&device, target.as_buffer_view())
        .unwrap();
    assert_eq!(
        target.read_to_vec(&device).unwrap(),
        vec![5.0, 6.0, 7.0, 8.0]
    );
}

#[test]
fn append_module_from_vmvx_fixture() {
    let vmfb = include_bytes!("mul_vmvx.vmfb");
    let output = run_mul(vmfb, "local-sync");
    assert_eq!(output[0], 0.0);
    assert_eq!(output[7], 49.0);
    assert_eq!(output[99], 9801.0);
}

#[test]
fn function_metadata_and_tensor_invoke() {
    let vmfb = include_bytes!("mul_vmvx.vmfb");
    let instance = runtime::vm::Instance::new().unwrap();
    let registry = runtime::hal::DriverRegistry::with_available_drivers().unwrap();
    let driver = registry.create_driver("local-sync").unwrap();
    let device = driver.create_default_device().unwrap();
    let hal_module = runtime::vm::Module::hal(&instance, &device).unwrap();
    let bytecode_module = runtime::vm::Module::bytecode(&instance, vmfb).unwrap();
    let context =
        runtime::vm::Context::with_modules(&instance, &[&hal_module, &bytecode_module]).unwrap();
    let function = context.resolve_function("arithmetic.simple_mul").unwrap();

    assert_eq!(bytecode_module.name(), "arithmetic");
    let module_signature = bytecode_module.signature();
    assert!(module_signature.export_function_count >= 1);
    assert!(bytecode_module.lookup_attr("missing.module.attr").is_none());
    assert!(bytecode_module
        .attr(module_signature.attr_count)
        .unwrap()
        .is_none());
    let function_ref = bytecode_module
        .lookup_export_function("simple_mul")
        .unwrap();
    assert_eq!(function_ref.name(), "simple_mul");
    assert_eq!(function_ref.signature().argument_count, 2);
    assert!(function_ref.lookup_attr("missing.function.attr").is_none());

    let function_ref = bytecode_module
        .lookup_function("simple_mul", FunctionLinkage::Export)
        .unwrap();
    assert_eq!(function_ref.name(), "simple_mul");

    assert_eq!(function.name(), "simple_mul");
    let signature = function.signature();
    assert_eq!(signature.argument_count, 2);
    assert_eq!(signature.result_count, 1);
    assert!(function.lookup_attr("missing.function.attr").is_none());

    let input_data = Vec::from_iter((0..100).map(|i| i as f32));
    let input = Tensor::<f32>::from_slice(&device, &[100], input_data.as_slice()).unwrap();
    let outputs = function.invoke_tensors(&[&input, &input], 1).unwrap();
    let output = outputs[0].read_to_vec(&device).unwrap();
    assert_eq!(output[0], 0.0);
    assert_eq!(output[7], 49.0);
    assert_eq!(output[99], 9801.0);
}

#[test]
fn invoke_after_newer_instance_rebinds_hal_types() {
    let vmfb = include_bytes!("mul_vmvx.vmfb");
    let instance = runtime::vm::Instance::new().unwrap();
    let registry = runtime::hal::DriverRegistry::with_available_drivers().unwrap();
    let driver = registry.create_driver("local-sync").unwrap();
    let device = driver.create_default_device().unwrap();
    let hal_module = runtime::vm::Module::hal(&instance, &device).unwrap();
    let bytecode_module = runtime::vm::Module::bytecode(&instance, vmfb).unwrap();
    let context =
        runtime::vm::Context::with_modules(&instance, &[&hal_module, &bytecode_module]).unwrap();
    let function = context.resolve_function("arithmetic.simple_mul").unwrap();

    let _newer_instance = runtime::vm::Instance::new().unwrap();

    let input_data = Vec::from_iter((0..100).map(|i| i as f32));
    let input = Tensor::<f32>::from_slice(&device, &[100], input_data.as_slice()).unwrap();
    let outputs = function.invoke_tensors(&[&input, &input], 1).unwrap();
    let output = outputs[0].read_to_vec(&device).unwrap();
    assert_eq!(output[0], 0.0);
    assert_eq!(output[7], 49.0);
    assert_eq!(output[99], 9801.0);
}

#[test]
fn parallel_instance_invocations_rebind_hal_types() {
    let mut threads = Vec::new();
    for _ in 0..2 {
        threads.push(std::thread::spawn(|| {
            let vmfb = include_bytes!("mul_vmvx.vmfb");
            let output = run_mul(vmfb, "local-sync");
            assert_eq!(output[0], 0.0);
            assert_eq!(output[7], 49.0);
            assert_eq!(output[99], 9801.0);
        }));
    }

    for thread in threads {
        thread.join().unwrap();
    }
}

fn run_mul(vmfb: &[u8], driver_name: &str) -> Vec<f32> {
    let instance = runtime::vm::Instance::new().unwrap();
    let registry = runtime::hal::DriverRegistry::with_available_drivers().unwrap();
    let driver = registry.create_driver(driver_name).unwrap();
    let device = driver.create_default_device().unwrap();
    let hal_module = runtime::vm::Module::hal(&instance, &device).unwrap();
    let bytecode_module = runtime::vm::Module::bytecode(&instance, vmfb).unwrap();
    let context =
        runtime::vm::Context::with_modules(&instance, &[&hal_module, &bytecode_module]).unwrap();
    let function = context.resolve_function("arithmetic.simple_mul").unwrap();

    let input_data = Vec::from_iter((0..100).map(|i| i as f32));
    let input = BufferView::<f32>::from_host(
        &device,
        &[100],
        Encoding::DenseRowMajor,
        input_data.as_slice(),
    )
    .unwrap();

    let mut input_list = List::<Undefined>::new(2, &instance).unwrap();
    input_list
        .push_ref(&input.to_ref(&instance).unwrap())
        .unwrap();
    input_list
        .push_ref(&input.to_ref(&instance).unwrap())
        .unwrap();
    let mut output_list = List::<Undefined>::new(1, &instance).unwrap();

    function.invoke(&input_list, &mut output_list).unwrap();

    let output_ref = output_list.get_ref::<BufferView<f32>>(0).unwrap();
    output_ref
        .to_buffer_view()
        .unwrap()
        .read_to_vec(&device)
        .unwrap()
}

#[cfg(feature = "compiler")]
mod integration_tests {
    use eerie::compiler;
    use log::info;
    use std::path::Path;
    use std::sync::Mutex;

    use super::run_mul;

    static COMPILER: Mutex<Option<compiler::Compiler>> = Mutex::new(None);

    fn init_compiler() {
        let mut global_compiler = COMPILER.lock().unwrap();
        if global_compiler.is_none() {
            let compiler = compiler::Compiler::new().unwrap();
            *global_compiler = Some(compiler);
        }
    }

    fn compile_mul(target_backend: &str) -> Vec<u8> {
        init_compiler();
        let compiler = COMPILER.lock().unwrap();
        let mut compiler_session = compiler.as_ref().unwrap().create_session();
        let mut flags = vec![format!("--iree-hal-target-backends={target_backend}")];
        if target_backend == "metal-spirv" {
            flags.push("--iree-metal-compile-to-metallib=false".to_string());
        }
        compiler_session.set_flags(flags).unwrap();
        let source = compiler_session
            .create_source_from_file(Path::new("tests/mul.mlir"))
            .unwrap();
        let mut invocation = compiler_session.create_invocation();
        let mut output = compiler::MemBufferOutput::new(compiler.as_ref().unwrap()).unwrap();
        invocation
            .parse_source(source)
            .unwrap()
            .set_verify_ir(true)
            .set_compile_to_phase("end")
            .unwrap()
            .pipeline(compiler::Pipeline::Std)
            .unwrap()
            .output_vm_byte_code(&mut output)
            .unwrap();
        output.map_memory().unwrap().to_vec()
    }

    #[test]
    fn append_module() {
        let vmfb = compile_mul("llvm-cpu");
        let output = run_mul(&vmfb, "local-sync");
        info!("Output: {:?}", output);
        assert_eq!(output[0], 0.0);
        assert_eq!(output[7], 49.0);
        assert_eq!(output[99], 9801.0);
    }

    #[test]
    fn metal_smoke() {
        if std::env::var("EERIE_TEST_METAL").as_deref() != Ok("1") {
            return;
        }

        let vmfb = compile_mul("metal-spirv");
        let output = run_mul(&vmfb, "metal");
        assert_eq!(output[0], 0.0);
        assert_eq!(output[7], 49.0);
        assert_eq!(output[99], 9801.0);
    }
}