WasmEdge Community Meeting: Wasi-crypto, Wasi-nn Proposal & India Devs Community

Jul 20, 2022 • 6 minutes to read

The WasmEdge maintainers host a monthly community meeting on the first Tuesday every month to share technical and community updates. On 5th July, the WasmEdge community discussed the runtime's implementation of wasi-nn and wasi-crypto proposals and the developer evangelizing in India for WasmEdge. Full video here.

It's worth mentioning that all the speakers in WasmEdge July community meeting are from the community. Anyone interested is more than welcome to speak and discuss!

Wasi-crypto Proposal

Sonder-joker, the WasmEdge contributor implementing the wasi-crypto proposal with the LFX mentorship, shared his work on wasi-crypto.

LFX mentorship builds the bridge between CNCF open source projects and potential contributors. It offers paid remote work for contributors.

As an open standard developed by a W3C group, WebAssembly's features are continuously enriched by implementing proposals. Wasi-crypto and wasi-nn are two of them.

From Sonder-joker's view, Wasi-crypto could help hardware acceleration and system-level secret isolation. Wasi-crypto can be used by WebAssembly code to perform cryptographic operations while preserving the sandbox nature of WebAssembly. For example, users can use wasi-crypto in various ways, like calculating hash and encryption.

Sonder-joker also shared the main coding style of wasi-crypto by showing three modules: asymmetric, key exchange and signatures. The asymmetric is base module for the other two modules.

  • Asymmetric operations. This module includes handle represent for key: public key, secret key and keypair. You can create them by calling (key type) key_ import() , and close them by calling key_close(), key_export() to copy raw key data from host side. You can also call functions in this module to transform these keys.
  • Key exchange. You can use classic Diffie-Hellman by pass pk and sk call kx_dh. It will produce the shared key. WasmEdge currently does not support key encapsulation exchange operation.
  • Signatures. It has a similar operation to symmetric state. It has two states: signature and verification signature. You can call (signatures/verification state)state_open to produce a new state, call ()state_update to absorb data source for signatures/verification, call signature_state_sign()/signature_verification_state_verify() to produce signatures /verification signatures, and call signatures/ verification _signsturs state_ to close them.
  • Symmetric State. If you want to execute symmetric relative function, then you need to use the symmetric_state_open() function. Then, call symetric_state_close () to clear it. If you want to have it cloned, call symmetric_state_clone(). The remaining symmetric_() like function is used to transform data if you want to symmetric encrypt or decrypt. Besides, most finalized operations do not destroy state: That means you can call symmetric_ relative finalized function many times.

As concluded, wasi-crypto is POSIX style API and handle-based. Its error code is based on the error handling mechanism.

The next step for wasi-crypto is to support TLS in its Rust SDK. See this issue for more details.

Wasi-nn Proposal

Gusy1234, the contributor implementing the wasi-nn proposal in WasmEdge with the LFX mentorship program, talked about the work done so far in supporting Wasi-nn proposal in WasmEdge.

Wasi-nn proposal is a high-level, “graph loader” API, shifting the work of compiling the models for the appropriate device to other tools (e.g. OpenVINO, PyTorch, Tensorflow). Wasi-nn only focuses on loading those compile models from different machine learning frameworks and AI inference.

Compared with the exiting WasmEdge TensorFlow extension, wasi-nn is more general.

Gusy1234 took a MobileNet model as an example and showed how to use wasi-nn to inference. Let's see the Rust code first. The Rust program is to load the model, create an execution instance of a loaded graph, define the tensor parameter. After that, the rust program computes and retrieves the results. See a full example here.

`
use wasi_nn;
mod imagenet_classes;
// load the model
let xml = fs::read_to_string(model_xml_name).unwrap();
    println!("Read graph XML, size in bytes: {}", xml.len());

    let weights = fs::read(model_bin_name).unwrap();
    println!("Read graph weights, size in bytes: {}", weights.len());

    let graph = unsafe {
        wasi_nn::load(
            &[&xml.into_bytes(), &weights],
            wasi_nn::GRAPH_ENCODING_OPENVINO,
            wasi_nn::EXECUTION_TARGET_CPU,
        )
        .unwrap()
    };
    
    ...
// create an execution instance
let context = unsafe { wasi_nn::init_execution_context(graph).unwrap() };
    println!("Created wasi-nn execution context with ID: {}", context);
// define tensor
let tensor_data = image_to_tensor(image_name.to_string(), 224, 224);
    println!("Read input tensor, size in bytes: {}", tensor_data.len());
    let tensor = wasi_nn::Tensor {
        dimensions: &[1, 3, 224, 224],
        r#type: wasi_nn::TENSOR_TYPE_F32,
        data: &tensor_data,
    };
// excute the inference
unsafe {
        wasi_nn::compute(context).unwrap();
    }
// retrieve the output
 let mut output_buffer = vec![0f32; 1001];
    unsafe {
        wasi_nn::get_output(
            context,
            0,
            &mut output_buffer[..] as *mut [f32] as *mut u8,
            (output_buffer.len() * 4).try_into().unwrap(),
        )
        .unwrap();
    }`

The next step is to build the .wasm file from the above rust code.

`
// generate .wasm file
$ cargo build --target=wasm32-wasi —release
// Turn on AOT mode to speed up image processing.
$ wasmedgec ./target/wasm32-wasi/release/wasinn-example.wasm wasinn-example.wasm`

The last step is to run the generated wasm with WasmEdge with the following command line. Then you will see the top 5 answers for the picture.

`
wasmedge --dir .:. wasinn-example.wasm mobilenet.xml mobilenet.bin input.jpg`

Now, wasi-nn proposal in WasmEdge has supported OpenVINO as the backend. In the future, gusy1234 will add more deep learning inference engines, like PyTorch, TensorRT, TensorFlow; And more interesting demos. Stay tuned!

Evangelizing WasmEdge in India

Shivay Lambda and Mritunjay Kumar Sharma from India shared how they plan to evangelize and attract more people's attention to WasmEdge and WebAssembly, and also expand the WasmEdge community in India. See the detailed proposal in this GitHub Issue.

According to the proposal, Shivay and Mritunjay will start with writing blogs and sharing Wasm articles on social media platforms like Twitter. They also plan to host face-to-face meetups and have WasmEdge participate in hackathons, and other types of developer-focused contests in India. Excited to see their evangelizing work in India. WasmEdge also created an India channel within its Discord server. Welcome to join!

Previous to the new evangelist proposal, Mritunjay had a talk- Paving the Way of WebAssembly in the Intersection of Machine Learning and Cloud Native on KubeCon + CloudNativeCon NA 2021. Shivay shared his take on Managing WebAssembly apps with Kubernetes on KubeCon + CloudNativeCon EU 2022.

Q&A

Q: You mentioned that you would be adding support later for Pytorch and ONNX. So is that currently being developed right now?

A: (By gusy1234) Yes. I'm working on it and trying to accomplish it in 3 months. Stay tuned. Keep an eye on the WasmEdge repo. You are also welcome to contribute some wasi-nn examples!

Q: What's WasmEdge's roadmap for the next 3 months?

A: (By Michael) We have the following planned.

  • Support the component model proposal (once named interface types) and the wit-bindgen proposal, which will make adding host function for WasmEdge easier. See this issue for progress.
  • Enhance networking support. That's because our goal really is to make WasmEdge the first-class citizen in a microservices environment.
  • Enhance JavaScript support and full Node.js APIs in JavaScript, which means developers can run JavaScript programs in WasmEdge without changes. See this issue for progress.
  • Improve the Rust SDK. Rust matters a lot in the Wasm ecosystem, which supports Rust the best. So WasmEdge works on perfecting its support for Rust!
  • Database driver could help wasm programs access the database, which is necessary for serverless functions. See this issue for progress.

Useful information

RustLFX mentorshipWebAssemblydevelopercommunitycontribution
A high-performance, extensible, and hardware optimized WebAssembly Virtual Machine for automotive, cloud, AI, and blockchain applications