Server-Side WebAssembly by Danilo Chiarlone
Author:Danilo Chiarlone [Danilo Chiarlone]
Language: eng
Format: epub
ISBN: 9781633436206
Publisher: Manning Publications Co.
Published: 2025-08-05T00:00:00+00:00
Overall, the full code can be seen in listing 5.9.
Listing 5.9 lib.rs file from smartcms_ml_guest
#[allow(warnings)] mod bindings; use tokenizers::Tokenizer; use rand::prelude::*; use ndarray::Array; use std::fs; use bindings::{Guest, wasi::nn::{ graph::{Graph, load, ExecutionTarget, GraphEncoding}, tensor::{Tensor, TensorType}, }}; struct Component; impl Guest for Component { fn storygen() -> String { let model_data = fs::read("fixture [CA]/modified_model.onnx").unwrap(); #A let graph = load(&[model_data], [CA]GraphEncoding::Onnx, ExecutionTarget [CA]::Cpu).unwrap(); #A let exec_context = Graph::init_execution_ [CA]context(&graph).unwrap(); #A let tokenizer = [CA]Tokenizer::from_file("fixture/ [CA]tokenizer.json").unwrap(); #B let prompt = "Once upon a time"; #C let encoding = tokenizer. [CA]encode(prompt, true).unwrap(); #C let mut input_ids = [CA]encoding.get_ids().to_vec(); #C let mut rng = thread_rng(); #D let eos_token_id = tokenizer.token_ [CA]to_id("<EOS>").unwrap_or(0); #D for _ in 0..100 { #E let sequence_length = input_ids.len(); #F let dimensions = vec! [CA][1, sequence_length as u32]; #F let input_f32: Vec<f32> = input_ [CA]ids.iter().map(|&id| id as f32).collect(); #F let mut input_data = Vec:: [CA]with_capacity(input_f32.len() * 4); #F for &val in &input_f32 { #F input_data.extend_from_slice [CA](&val.to_ne_bytes()); #F } #F let tensor = Tensor::new [CA](&dimensions, TensorType::Fp32, &input_data); #F exec_context.set_input [CA]("input_ids", tensor).unwrap(); #F exec_context.compute().unwrap(); #G let output_data = exec_context. [CA]get_output("logits").unwrap().data(); #H let output_f32 = bytes_to_f32_vec [CA](&output_data); #H let vocab_size = output_f32. [CA]len() / sequence_length; #I let start = (sequence_length - 1) [CA]* vocab_size; #I let end = sequence_length * vocab_size; #I let last_token_logits = [CA]&output_f32[start..end]; #I let probabilities = softmax [CA](last_token_logits); #J let dist = rand::distributions:: [CA]WeightedIndex::new(&probabilities).unwrap(); #K let next_token = dist.sample(&mut rng); #K input_ids.push(next_token as u32); #K let generated_text = [CA]tokenizer.decode(&input_ids, true).unwrap(); #L if generated_text.ends_with("
") { #M break; #M } #M if next_token as u32 == eos_token_id { #M break; #M } #M } tokenizer.decode(&input_ids, true) [CA].unwrap().trim().to_string() #N } } pub fn bytes_to_f32_vec(data: &[u8]) -> Vec<f32> { #O data.chunks_exact(4) .map(|c| f32::from_le_bytes(c.try_into().unwrap())) .collect() } fn softmax(logits: &[f32]) -> Vec<f32> { #P let logits_array = Array::from_vec(logits.to_vec()); let exp_logits = logits_array.mapv(f32::exp); let sum_exp = exp_logits.sum(); (exp_logits / sum_exp).to_vec() } bindings::export!(Component with_types_in bindings);
#A Loading the ONNX model and setting up the graph and execution context
#B Loading the tokenizer from the provided file
#C Defining the prompt and encoding it into token IDs
#D Initializing the random number generator and setting the EOS token
#E Looping up to 100 iterations to generate tokens
#F Preparing and setting the input tensor
#G Running the model inference
#H Retrieving and converting the modelâs output
#I Extracting the logits for the last token in the sequence
#J Applying softmax to calculate token probabilities
#K Sampling the next token based on probabilities and appending it to the sequence
#L Decoding the generated text
#M Breaking the loop if a natural end to the story is detected
#N Returning the final generated story
#O The function to convert bytes to f32 values
#P The softmax function to compute the probability distribution
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26958)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(20855)
De Souza H. Master the Age of Artificial Intelligences. The Basic Guide...2024 by Unknown(20611)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(20536)
The Fifty Shades Trilogy & Grey by E L James(19456)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19378)
Shot Through the Heart by Mercy Celeste(19241)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17386)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(17354)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(17184)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(17098)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16713)
The Subtle Art of Not Giving a F*ck by Mark Manson(14825)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14443)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(14217)
Cozy crochet hats: 7 Stylish and Beginner-Friendly Patterns from Baby Beanies to Trendy Bucket Hats by Vanilla Lazy(13500)
Scorched Earth by Nick Kyme(13096)
Reichel W. Numerical methods for Electrical Engineering, Meteorology,...2022 by Unknown(12976)
Drei Generationen auf dem Jakobsweg by Stein Pia(11258)