Instructions to use casawolice/small100-onnx with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers.js
How to use casawolice/small100-onnx with Transformers.js:
// npm i @huggingface/transformers import { pipeline } from '@huggingface/transformers'; // Allocate pipeline const pipe = await pipeline('translation', 'casawolice/small100-onnx');
Upload SMaLL-100 universal ONNX (int8 model, tokenizer.json, lang map, 4-platform examples)
a0d8498 verified Download examples/ios/Small100Translator.swift from casawolice/small100-onnx: direct link, hf CLI and curl.
- Browser
- Download file 3.64 kB
-
https://huggingface.co/casawolice/small100-onnx/resolve/main/examples/ios/Small100Translator.swift
- Command line
-
hf download hf://casawolice/small100-onnx/examples/ios/Small100Translator.swift
-
curl -L -o Small100Translator.swift https://huggingface.co/casawolice/small100-onnx/resolve/main/examples/ios/Small100Translator.swift
3.64 kB
| // Reference SMaLL-100 translator for iOS (onnxruntime-swift + swift-transformers). | |
| // | |
| // Swift Package deps: | |
| // .package(url: "https://github.com/microsoft/onnxruntime-swift-package-manager", from: "1.20.0") | |
| // .package(url: "https://github.com/huggingface/swift-transformers", from: "0.1.8") | |
| // | |
| // Bundle onnx/encoder_model.onnx, onnx/decoder_model_merged.onnx, tokenizer.json, | |
| // lang_tokens.json. This is a reference implementation of USAGE.md; adjust to your | |
| // onnxruntime-swift / swift-transformers versions. | |
| import Foundation | |
| import onnxruntime_objc | |
| import Tokenizers // from swift-transformers | |
| final class Small100Translator { | |
| private let env = try! ORTEnv(loggingLevel: .warning) | |
| private let encoder: ORTSession | |
| private let decoder: ORTSession | |
| private let tokenizer: Tokenizer // loaded from tokenizer.json | |
| private let langToId: [String: Int64] | |
| private let nLayers = 3, eos: Int64 = 2, decoderStart: Int64 = 2 | |
| init(encoderPath: String, decoderPath: String, tokenizer: Tokenizer, langToId: [String: Int64]) throws { | |
| let opts = try ORTSessionOptions() | |
| self.encoder = try ORTSession(env: env, modelPath: encoderPath, sessionOptions: opts) | |
| self.decoder = try ORTSession(env: env, modelPath: decoderPath, sessionOptions: opts) | |
| self.tokenizer = tokenizer | |
| self.langToId = langToId | |
| } | |
| func translate(_ text: String, to tgt: String, maxLen: Int = 128) throws -> String { | |
| // 1) ids = [tgt lang token] + tokenizer(text) (tokenizer appends </s>=2) | |
| var ids: [Int64] = [langToId[tgt]!] | |
| ids += tokenizer.encode(text: text).map { Int64($0) } | |
| let mask = [Int64](repeating: 1, count: ids.count) | |
| // 2) encoder | |
| let idsT = try tensor(ids, [1, ids.count]) | |
| let maskT = try tensor(mask, [1, mask.count]) | |
| let encOut = try encoder.run(withInputs: ["input_ids": idsT, "attention_mask": maskT], | |
| outputNames: ["last_hidden_state"], runOptions: nil) | |
| let encHidden = encOut["last_hidden_state"]! | |
| // 3) greedy decode over merged decoder (KV cache) | |
| var out: [Int] = [] | |
| var cur = decoderStart, useCache = false | |
| var past = try emptyPast() | |
| for _ in 0..<maxLen { | |
| var feeds: [String: ORTValue] = [ | |
| "encoder_attention_mask": maskT, | |
| "input_ids": try tensor([cur], [1, 1]), | |
| "encoder_hidden_states": encHidden, | |
| "use_cache_branch": try boolTensor(useCache), | |
| ] | |
| past.forEach { feeds[$0.key] = $0.value } | |
| let res = try decoder.run(withInputs: feeds, | |
| outputNames: decoderOutputNames(), runOptions: nil) | |
| let next = try argmaxLastLogits(res["logits"]!) | |
| if next == eos { break } | |
| out.append(Int(next)) | |
| past = rollPast(res, wasCache: useCache, prev: past) | |
| cur = next; useCache = true | |
| } | |
| return tokenizer.decode(tokens: out) | |
| } | |
| // --- helpers (tensor creation, KV rolling, argmax) omitted for brevity --- | |
| // tensor(_:)/boolTensor(_:): wrap [Int64]/Bool into ORTValue(tensorData:elementType:.int64/.bool shape:). | |
| // emptyPast(): 3 layers × {decoder,encoder} × {key,value} float tensors of shape [1,16,0,64]. | |
| // rollPast(): present.i.decoder.* every step; present.i.encoder.* only when !wasCache, else keep prev. | |
| // argmaxLastLogits(): argmax over the last position of logits [1, seq, 128112]. | |
| // See USAGE.md and examples/android/Small100Translator.kt for the exact mapping. | |
| } | |