Views
No views yet
flan_t5_base_encoder_quality.mlpackage - T5 Encoder component (512 tokens, FP32, 430MB)flan_t5_base_decoder_quality.mlpackage - T5 Decoder component (512 tokens, FP32, 647MB)flan_t5_base_encoder_int8.mlpackage - T5 Encoder component (512 tokens, INT8, 108MB)flan_t5_base_decoder_int8.mlpackage - T5 Decoder component (512 tokens, INT8, 164MB)tokenizer.json - Fast tokenizer configurationtokenizer_config.json - Tokenizer metadata and settingsspecial_tokens_map.json - Special token mappingsspiece.model - SentencePiece model for tokenizationinput_ids (shape: [1, 512], dtype: int32), attention_mask (shape: [1, 512], dtype: int32)hidden_states (shape: [1, 512, 768], dtype: float32)decoder_input_ids (shape: [1, 512], dtype: int32)encoder_hidden_states (shape: [1, 512, 768], dtype: float32)decoder_attention_mask (shape: [1, 512], dtype: int32)encoder_attention_mask (shape: [1, 512], dtype: int32)logits (shape: [1, 512, 32128], dtype: float32)| Model Type | Size | Use Case | Quality | Memory |
|---|---|---|---|---|
| FP32 Quality | 1.1GB | Server/Desktop apps, Research | Highest | High |
| INT8 Mobile | 272MB | iOS/Mobile apps, Production | Very Good | Low |
1# Download complete repository
2huggingface-cli download mazhewitt/flan-t5-base-coreml --local-dir ./models
3
4# Download specific models (choose quality vs mobile-optimized)
5# High-quality FP32 models
6huggingface-cli download mazhewitt/flan-t5-base-coreml flan_t5_base_encoder_quality.mlpackage --local-dir ./models
7huggingface-cli download mazhewitt/flan-t5-base-coreml flan_t5_base_decoder_quality.mlpackage --local-dir ./models
8
9# Mobile-optimized INT8 models (recommended for iOS/mobile apps)
10huggingface-cli download mazhewitt/flan-t5-base-coreml flan_t5_base_encoder_int8.mlpackage --local-dir ./models
11huggingface-cli download mazhewitt/flan-t5-base-coreml flan_t5_base_decoder_int8.mlpackage --local-dir ./models1import coremltools as ct
2import numpy as np
3from transformers import T5Tokenizer
4
5# Load models and tokenizer
6# Option 1: High-quality FP32 models (1.1GB)
7encoder = ct.models.MLModel("flan_t5_base_encoder_quality.mlpackage")
8decoder = ct.models.MLModel("flan_t5_base_decoder_quality.mlpackage")
9
10# Option 2: Mobile-optimized INT8 models (272MB) - Recommended for iOS apps
11# encoder = ct.models.MLModel("flan_t5_base_encoder_int8.mlpackage")
12# decoder = ct.models.MLModel("flan_t5_base_decoder_int8.mlpackage")
13
14tokenizer = T5Tokenizer.from_pretrained("./")
15
16# Example: Translation with high-quality generation
17input_text = "translate English to French: Hello world"
18inputs = tokenizer(input_text, return_tensors="np", padding="max_length",
19 truncation=True, max_length=512)
20
21# Run encoder
22encoder_output = encoder.predict({
23 "input_ids": inputs["input_ids"].astype(np.int32),
24 "attention_mask": inputs["attention_mask"].astype(np.int32)
25})
26hidden_states = encoder_output["hidden_states"]
27
28# Greedy generation (working causal attention)
29generated_tokens = [tokenizer.pad_token_id] # Start with pad token
30max_new_tokens = 10
31
32for _ in range(max_new_tokens):
33 # Prepare decoder input
34 decoder_ids = np.zeros((1, 512), dtype=np.int32)
35 decoder_mask = np.zeros((1, 512), dtype=np.int32)
36
37 for i, token in enumerate(generated_tokens):
38 decoder_ids[0, i] = token
39 decoder_mask[0, i] = 1
40
41 # Run decoder
42 decoder_output = decoder.predict({
43 "decoder_input_ids": decoder_ids,
44 "encoder_hidden_states": hidden_states,
45 "decoder_attention_mask": decoder_mask,
46 "encoder_attention_mask": inputs["attention_mask"].astype(np.int32)
47 })
48
49 # Get next token
50 next_pos = len(generated_tokens)
51 logits = decoder_output["logits"]
52 next_token = np.argmax(logits[0, next_pos, :])
53
54 # Stop if EOS token
55 if next_token == tokenizer.eos_token_id:
56 break
57
58 generated_tokens.append(int(next_token))
59
60# Decode result (skip initial pad token)
61result = tokenizer.decode(generated_tokens[1:], skip_special_tokens=True)
62print(f"Translation: {result}")1import CoreML
2
3// Load models
4// Option 1: High-quality FP32 models
5guard let encoderURL = Bundle.main.url(forResource: "flan_t5_base_encoder_quality", withExtension: "mlpackage"),
6 let decoderURL = Bundle.main.url(forResource: "flan_t5_base_decoder_quality", withExtension: "mlpackage") else {
7 fatalError("Models not found")
8}
9
10// Option 2: Mobile-optimized INT8 models (recommended for iOS apps)
11// guard let encoderURL = Bundle.main.url(forResource: "flan_t5_base_encoder_int8", withExtension: "mlpackage"),
12// let decoderURL = Bundle.main.url(forResource: "flan_t5_base_decoder_int8", withExtension: "mlpackage") else {
13 fatalError("Models not found")
14}
15
16let encoderModel = try MLModel(contentsOf: encoderURL)
17let decoderModel = try MLModel(contentsOf: decoderURL)
18
19// Example inference (similar pattern to Python but with MLMultiArray)
20// Note: You'll need to implement tokenization in Swift or use a bridging approachtokenizer.pad_token_id for decoder input1# This should produce DIFFERENT results (proving causal attention works)
2context_1 = [tokenizer.pad_token_id, 1000] # Different token at position 1
3context_2 = [tokenizer.pad_token_id, 2000] # Different token at position 1
4# Running decoder with these contexts should give different predictions1@article{chung2022scaling,
2 title={Scaling instruction-finetuned language models},
3 author={Chung, Hyung Won and Hou, Le and Longpre, Shayne and Zoph, Barret and Tay, Yi and Fedus, William and Li, Eric and Wang, Xuezhi and Mostafazadeh, Nasrin and Shen, Jianmo and others},
4 journal={arXiv preprint arXiv:2210.11416},
5 year={2022}
6}