[PoC] Transformers contract: run every input case (text, image, text + image)

#5
by hf-transformers-bot HF Staff - opened
integration.json CHANGED
@@ -1,7 +1,7 @@
1
  {
2
  "mic": "0.3",
3
  "name": "Gemma3 causal LM",
4
- "usage": "AutoProcessor + AutoModelForCausalLM, text-only batched with padding, one training step",
5
  "architecture": "Gemma3ForConditionalGeneration",
6
  "checkpoints": {
7
  "google/gemma-3-4b-it": {
@@ -70,13 +70,35 @@
70
  "runtime": {
71
  "compare": "exact"
72
  },
73
- "predicted_tokens": {
74
  "compare": "exact"
75
  },
76
- "predicted_text": {
77
  "compare": "exact"
78
  },
79
- "answer_logits": {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  "compare": "allclose",
81
  "atol": 0.0001,
82
  "rtol": 0.0001
 
1
  {
2
  "mic": "0.3",
3
  "name": "Gemma3 causal LM",
4
+ "usage": "AutoProcessor + AutoModelForCausalLM, every input case (text batched with padding, image, text + image), one training step",
5
  "architecture": "Gemma3ForConditionalGeneration",
6
  "checkpoints": {
7
  "google/gemma-3-4b-it": {
 
70
  "runtime": {
71
  "compare": "exact"
72
  },
73
+ "cases.text.predicted_tokens": {
74
  "compare": "exact"
75
  },
76
+ "cases.text.predicted_text": {
77
  "compare": "exact"
78
  },
79
+ "cases.text.answer_logits": {
80
+ "compare": "allclose",
81
+ "atol": 0.0001,
82
+ "rtol": 0.0001
83
+ },
84
+ "cases.image.predicted_tokens": {
85
+ "compare": "exact"
86
+ },
87
+ "cases.image.predicted_text": {
88
+ "compare": "exact"
89
+ },
90
+ "cases.image.answer_logits": {
91
+ "compare": "allclose",
92
+ "atol": 0.0001,
93
+ "rtol": 0.0001
94
+ },
95
+ "cases.text_image.predicted_tokens": {
96
+ "compare": "exact"
97
+ },
98
+ "cases.text_image.predicted_text": {
99
+ "compare": "exact"
100
+ },
101
+ "cases.text_image.answer_logits": {
102
  "compare": "allclose",
103
  "atol": 0.0001,
104
  "rtol": 0.0001
integrations/transformers/expectations/cuda/gemma3-mic.bfloat16.json CHANGED
@@ -2,26 +2,26 @@
2
  "recorded_with": {
3
  "contract": "huggingface-mic/gemma3-mic",
4
  "contract_name": "mic-gemma3",
5
- "contract_revision": "4006790f6abe8bbd2488f2e18e786e2c24980966",
6
  "framework": "transformers",
7
  "variant": "bfloat16",
8
  "target": "gemma3-mic",
9
  "kind": "fixture",
10
- "revision": "4006790f6abe8bbd2488f2e18e786e2c24980966",
11
  "source_model": "google/gemma-3-4b-it",
12
  "weights": "random",
13
  "fixture_repo": "huggingface-mic/gemma3-mic",
14
  "environment": {
15
  "python": "3.11.18",
16
- "transformers": "5.23.0.dev0",
17
  "sentence-transformers": null,
18
- "torch": "2.15.0+cu132",
19
- "torchvision": "0.29.2+cu132",
20
- "torchaudio": "2.11.1+cu132",
21
  "tokenizers": "0.23.3",
22
- "numpy": "2.4.6",
23
  "pillow": "13.0.0",
24
- "accelerate": "1.16.0.dev0",
25
  "safetensors": "0.8.0",
26
  "huggingface-hub": "2.4.0",
27
  "sentencepiece": "0.2.3",
@@ -30,18 +30,17 @@
30
  "soundfile": "0.14.0",
31
  "scipy": "1.17.1"
32
  },
33
- "seconds": 8.77,
34
  "environment_differs": {
35
- "torch": "2.15.0+cu132 (pinned 2.14.0)",
36
- "torchvision": "0.29.2+cu132 (pinned 0.29.1)",
37
- "torchaudio": "2.11.1+cu132 (pinned 2.11.0)",
38
- "numpy": "2.4.6 (pinned 2.3.5)",
39
  "pillow": "13.0.0 (pinned 12.3.0)",
40
- "accelerate": "1.16.0.dev0 (pinned 1.15.0)",
41
  "huggingface-hub": "2.4.0 (pinned 2.3.0)",
42
  "sentencepiece": "0.2.3 (pinned 0.2.2)"
43
  },
44
  "platform": "cuda",
 
 
 
45
  "template_date": "2026-01-01"
46
  },
47
  "outputs": {
@@ -54,23 +53,53 @@
54
  "vision_config": "sdpa"
55
  }
56
  },
57
- "predicted_tokens": [
58
- "\u2581\u0987\u09a8\u09cd\u09a6\u09c1",
59
- "\u0648\u0627\u062c"
60
- ],
61
- "predicted_text": " \u0987\u09a8\u09cd\u09a6\u09c1\u0648\u0627\u062c",
62
- "answer_logits": [
63
- [
64
- 0.6796875,
65
- 0.66796875,
66
- 0.6640625
67
- ],
68
- [
69
- 0.71875,
70
- 0.66015625,
71
- 0.65625
72
- ]
73
- ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  "training": {
75
  "loss": 12.520472526550293,
76
  "gradients": {
 
2
  "recorded_with": {
3
  "contract": "huggingface-mic/gemma3-mic",
4
  "contract_name": "mic-gemma3",
5
+ "contract_revision": "unpublished",
6
  "framework": "transformers",
7
  "variant": "bfloat16",
8
  "target": "gemma3-mic",
9
  "kind": "fixture",
10
+ "revision": "7987697a4ca53c8cb7217b7f82c891cf5e6ad09a",
11
  "source_model": "google/gemma-3-4b-it",
12
  "weights": "random",
13
  "fixture_repo": "huggingface-mic/gemma3-mic",
14
  "environment": {
15
  "python": "3.11.18",
16
+ "transformers": "5.24.0.dev0",
17
  "sentence-transformers": null,
18
+ "torch": "2.15.0",
19
+ "torchvision": "0.29.2",
20
+ "torchaudio": "2.11.1",
21
  "tokenizers": "0.23.3",
22
+ "numpy": "2.3.5",
23
  "pillow": "13.0.0",
24
+ "accelerate": "1.16.0",
25
  "safetensors": "0.8.0",
26
  "huggingface-hub": "2.4.0",
27
  "sentencepiece": "0.2.3",
 
30
  "soundfile": "0.14.0",
31
  "scipy": "1.17.1"
32
  },
33
+ "seconds": 6.84,
34
  "environment_differs": {
 
 
 
 
35
  "pillow": "13.0.0 (pinned 12.3.0)",
36
+ "accelerate": "1.16.0 (pinned 1.15.0)",
37
  "huggingface-hub": "2.4.0 (pinned 2.3.0)",
38
  "sentencepiece": "0.2.3 (pinned 0.2.2)"
39
  },
40
  "platform": "cuda",
41
+ "baseline_repo": "huggingface-mic/gemma3-mic",
42
+ "baseline_revision": "unpublished",
43
+ "baseline_path": "integrations/transformers/expectations/cuda/gemma3-mic.bfloat16.json",
44
  "template_date": "2026-01-01"
45
  },
46
  "outputs": {
 
53
  "vision_config": "sdpa"
54
  }
55
  },
56
+ "cases": {
57
+ "text": {
58
+ "predicted_tokens": [
59
+ "\u2581\u0987\u09a8\u09cd\u09a6\u09c1",
60
+ "\u0648\u0627\u062c"
61
+ ],
62
+ "predicted_text": " \u0987\u09a8\u09cd\u09a6\u09c1\u0648\u0627\u062c",
63
+ "answer_logits": [
64
+ [
65
+ 0.6796875,
66
+ 0.66796875,
67
+ 0.6640625
68
+ ],
69
+ [
70
+ 0.71875,
71
+ 0.66015625,
72
+ 0.65625
73
+ ]
74
+ ]
75
+ },
76
+ "image": {
77
+ "predicted_tokens": [
78
+ "\u10d4\u10d1\u10e3\u10da\u10d8\u10d0"
79
+ ],
80
+ "predicted_text": "\u10d4\u10d1\u10e3\u10da\u10d8\u10d0",
81
+ "answer_logits": [
82
+ [
83
+ 0.70703125,
84
+ 0.703125,
85
+ 0.69921875
86
+ ]
87
+ ]
88
+ },
89
+ "text_image": {
90
+ "predicted_tokens": [
91
+ "\u10d4\u10d1\u10e3\u10da\u10d8\u10d0"
92
+ ],
93
+ "predicted_text": "\u10d4\u10d1\u10e3\u10da\u10d8\u10d0",
94
+ "answer_logits": [
95
+ [
96
+ 0.84765625,
97
+ 0.765625,
98
+ 0.66796875
99
+ ]
100
+ ]
101
+ }
102
+ },
103
  "training": {
104
  "loss": 12.520472526550293,
105
  "gradients": {
integrations/transformers/expectations/cuda/gemma3-mic.eager.json CHANGED
@@ -2,26 +2,26 @@
2
  "recorded_with": {
3
  "contract": "huggingface-mic/gemma3-mic",
4
  "contract_name": "mic-gemma3",
5
- "contract_revision": "4006790f6abe8bbd2488f2e18e786e2c24980966",
6
  "framework": "transformers",
7
  "variant": "eager",
8
  "target": "gemma3-mic",
9
  "kind": "fixture",
10
- "revision": "4006790f6abe8bbd2488f2e18e786e2c24980966",
11
  "source_model": "google/gemma-3-4b-it",
12
  "weights": "random",
13
  "fixture_repo": "huggingface-mic/gemma3-mic",
14
  "environment": {
15
  "python": "3.11.18",
16
- "transformers": "5.23.0.dev0",
17
  "sentence-transformers": null,
18
- "torch": "2.15.0+cu132",
19
- "torchvision": "0.29.2+cu132",
20
- "torchaudio": "2.11.1+cu132",
21
  "tokenizers": "0.23.3",
22
- "numpy": "2.4.6",
23
  "pillow": "13.0.0",
24
- "accelerate": "1.16.0.dev0",
25
  "safetensors": "0.8.0",
26
  "huggingface-hub": "2.4.0",
27
  "sentencepiece": "0.2.3",
@@ -30,18 +30,17 @@
30
  "soundfile": "0.14.0",
31
  "scipy": "1.17.1"
32
  },
33
- "seconds": 8.74,
34
  "environment_differs": {
35
- "torch": "2.15.0+cu132 (pinned 2.14.0)",
36
- "torchvision": "0.29.2+cu132 (pinned 0.29.1)",
37
- "torchaudio": "2.11.1+cu132 (pinned 2.11.0)",
38
- "numpy": "2.4.6 (pinned 2.3.5)",
39
  "pillow": "13.0.0 (pinned 12.3.0)",
40
- "accelerate": "1.16.0.dev0 (pinned 1.15.0)",
41
  "huggingface-hub": "2.4.0 (pinned 2.3.0)",
42
  "sentencepiece": "0.2.3 (pinned 0.2.2)"
43
  },
44
  "platform": "cuda",
 
 
 
45
  "template_date": "2026-01-01"
46
  },
47
  "outputs": {
@@ -54,23 +53,53 @@
54
  "vision_config": "eager"
55
  }
56
  },
57
- "predicted_tokens": [
58
- "\u2581\u0987\u09a8\u09cd\u09a6\u09c1",
59
- "\u0648\u0627\u062c"
60
- ],
61
- "predicted_text": " \u0987\u09a8\u09cd\u09a6\u09c1\u0648\u0627\u062c",
62
- "answer_logits": [
63
- [
64
- 0.67578125,
65
- 0.66796875,
66
- 0.66796875
67
- ],
68
- [
69
- 0.71875,
70
- 0.66015625,
71
- 0.66015625
72
- ]
73
- ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  "training": {
75
  "loss": 12.525347709655762,
76
  "gradients": {
 
2
  "recorded_with": {
3
  "contract": "huggingface-mic/gemma3-mic",
4
  "contract_name": "mic-gemma3",
5
+ "contract_revision": "unpublished",
6
  "framework": "transformers",
7
  "variant": "eager",
8
  "target": "gemma3-mic",
9
  "kind": "fixture",
10
+ "revision": "7987697a4ca53c8cb7217b7f82c891cf5e6ad09a",
11
  "source_model": "google/gemma-3-4b-it",
12
  "weights": "random",
13
  "fixture_repo": "huggingface-mic/gemma3-mic",
14
  "environment": {
15
  "python": "3.11.18",
16
+ "transformers": "5.24.0.dev0",
17
  "sentence-transformers": null,
18
+ "torch": "2.15.0",
19
+ "torchvision": "0.29.2",
20
+ "torchaudio": "2.11.1",
21
  "tokenizers": "0.23.3",
22
+ "numpy": "2.3.5",
23
  "pillow": "13.0.0",
24
+ "accelerate": "1.16.0",
25
  "safetensors": "0.8.0",
26
  "huggingface-hub": "2.4.0",
27
  "sentencepiece": "0.2.3",
 
30
  "soundfile": "0.14.0",
31
  "scipy": "1.17.1"
32
  },
33
+ "seconds": 6.53,
34
  "environment_differs": {
 
 
 
 
35
  "pillow": "13.0.0 (pinned 12.3.0)",
36
+ "accelerate": "1.16.0 (pinned 1.15.0)",
37
  "huggingface-hub": "2.4.0 (pinned 2.3.0)",
38
  "sentencepiece": "0.2.3 (pinned 0.2.2)"
39
  },
40
  "platform": "cuda",
41
+ "baseline_repo": "huggingface-mic/gemma3-mic",
42
+ "baseline_revision": "unpublished",
43
+ "baseline_path": "integrations/transformers/expectations/cuda/gemma3-mic.eager.json",
44
  "template_date": "2026-01-01"
45
  },
46
  "outputs": {
 
53
  "vision_config": "eager"
54
  }
55
  },
56
+ "cases": {
57
+ "text": {
58
+ "predicted_tokens": [
59
+ "\u2581\u0987\u09a8\u09cd\u09a6\u09c1",
60
+ "\u0648\u0627\u062c"
61
+ ],
62
+ "predicted_text": " \u0987\u09a8\u09cd\u09a6\u09c1\u0648\u0627\u062c",
63
+ "answer_logits": [
64
+ [
65
+ 0.67578125,
66
+ 0.66796875,
67
+ 0.66796875
68
+ ],
69
+ [
70
+ 0.71875,
71
+ 0.66015625,
72
+ 0.66015625
73
+ ]
74
+ ]
75
+ },
76
+ "image": {
77
+ "predicted_tokens": [
78
+ "\u10d4\u10d1\u10e3\u10da\u10d8\u10d0"
79
+ ],
80
+ "predicted_text": "\u10d4\u10d1\u10e3\u10da\u10d8\u10d0",
81
+ "answer_logits": [
82
+ [
83
+ 0.70703125,
84
+ 0.703125,
85
+ 0.69921875
86
+ ]
87
+ ]
88
+ },
89
+ "text_image": {
90
+ "predicted_tokens": [
91
+ "\u10d4\u10d1\u10e3\u10da\u10d8\u10d0"
92
+ ],
93
+ "predicted_text": "\u10d4\u10d1\u10e3\u10da\u10d8\u10d0",
94
+ "answer_logits": [
95
+ [
96
+ 0.84375,
97
+ 0.76171875,
98
+ 0.671875
99
+ ]
100
+ ]
101
+ }
102
+ },
103
  "training": {
104
  "loss": 12.525347709655762,
105
  "gradients": {
integrations/transformers/expectations/cuda/gemma3-mic.json CHANGED
@@ -2,26 +2,26 @@
2
  "recorded_with": {
3
  "contract": "huggingface-mic/gemma3-mic",
4
  "contract_name": "mic-gemma3",
5
- "contract_revision": "4006790f6abe8bbd2488f2e18e786e2c24980966",
6
  "framework": "transformers",
7
  "variant": "default",
8
  "target": "gemma3-mic",
9
  "kind": "fixture",
10
- "revision": "4006790f6abe8bbd2488f2e18e786e2c24980966",
11
  "source_model": "google/gemma-3-4b-it",
12
  "weights": "random",
13
  "fixture_repo": "huggingface-mic/gemma3-mic",
14
  "environment": {
15
  "python": "3.11.18",
16
- "transformers": "5.23.0.dev0",
17
  "sentence-transformers": null,
18
- "torch": "2.15.0+cu132",
19
- "torchvision": "0.29.2+cu132",
20
- "torchaudio": "2.11.1+cu132",
21
  "tokenizers": "0.23.3",
22
- "numpy": "2.4.6",
23
  "pillow": "13.0.0",
24
- "accelerate": "1.16.0.dev0",
25
  "safetensors": "0.8.0",
26
  "huggingface-hub": "2.4.0",
27
  "sentencepiece": "0.2.3",
@@ -30,18 +30,17 @@
30
  "soundfile": "0.14.0",
31
  "scipy": "1.17.1"
32
  },
33
- "seconds": 9.22,
34
  "environment_differs": {
35
- "torch": "2.15.0+cu132 (pinned 2.14.0)",
36
- "torchvision": "0.29.2+cu132 (pinned 0.29.1)",
37
- "torchaudio": "2.11.1+cu132 (pinned 2.11.0)",
38
- "numpy": "2.4.6 (pinned 2.3.5)",
39
  "pillow": "13.0.0 (pinned 12.3.0)",
40
- "accelerate": "1.16.0.dev0 (pinned 1.15.0)",
41
  "huggingface-hub": "2.4.0 (pinned 2.3.0)",
42
  "sentencepiece": "0.2.3 (pinned 0.2.2)"
43
  },
44
  "platform": "cuda",
 
 
 
45
  "template_date": "2026-01-01"
46
  },
47
  "outputs": {
@@ -54,23 +53,53 @@
54
  "vision_config": "sdpa"
55
  }
56
  },
57
- "predicted_tokens": [
58
- "\u2581\u0987\u09a8\u09cd\u09a6\u09c1",
59
- "\u0648\u0627\u062c"
60
- ],
61
- "predicted_text": " \u0987\u09a8\u09cd\u09a6\u09c1\u0648\u0627\u062c",
62
- "answer_logits": [
63
- [
64
- 0.6796875,
65
- 0.66796875,
66
- 0.6640625
67
- ],
68
- [
69
- 0.71875,
70
- 0.66015625,
71
- 0.65625
72
- ]
73
- ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  "training": {
75
  "loss": 12.520472526550293,
76
  "gradients": {
 
2
  "recorded_with": {
3
  "contract": "huggingface-mic/gemma3-mic",
4
  "contract_name": "mic-gemma3",
5
+ "contract_revision": "unpublished",
6
  "framework": "transformers",
7
  "variant": "default",
8
  "target": "gemma3-mic",
9
  "kind": "fixture",
10
+ "revision": "7987697a4ca53c8cb7217b7f82c891cf5e6ad09a",
11
  "source_model": "google/gemma-3-4b-it",
12
  "weights": "random",
13
  "fixture_repo": "huggingface-mic/gemma3-mic",
14
  "environment": {
15
  "python": "3.11.18",
16
+ "transformers": "5.24.0.dev0",
17
  "sentence-transformers": null,
18
+ "torch": "2.15.0",
19
+ "torchvision": "0.29.2",
20
+ "torchaudio": "2.11.1",
21
  "tokenizers": "0.23.3",
22
+ "numpy": "2.3.5",
23
  "pillow": "13.0.0",
24
+ "accelerate": "1.16.0",
25
  "safetensors": "0.8.0",
26
  "huggingface-hub": "2.4.0",
27
  "sentencepiece": "0.2.3",
 
30
  "soundfile": "0.14.0",
31
  "scipy": "1.17.1"
32
  },
33
+ "seconds": 6.93,
34
  "environment_differs": {
 
 
 
 
35
  "pillow": "13.0.0 (pinned 12.3.0)",
36
+ "accelerate": "1.16.0 (pinned 1.15.0)",
37
  "huggingface-hub": "2.4.0 (pinned 2.3.0)",
38
  "sentencepiece": "0.2.3 (pinned 0.2.2)"
39
  },
40
  "platform": "cuda",
41
+ "baseline_repo": "huggingface-mic/gemma3-mic",
42
+ "baseline_revision": "unpublished",
43
+ "baseline_path": "integrations/transformers/expectations/cuda/gemma3-mic.json",
44
  "template_date": "2026-01-01"
45
  },
46
  "outputs": {
 
53
  "vision_config": "sdpa"
54
  }
55
  },
56
+ "cases": {
57
+ "text": {
58
+ "predicted_tokens": [
59
+ "\u2581\u0987\u09a8\u09cd\u09a6\u09c1",
60
+ "\u0648\u0627\u062c"
61
+ ],
62
+ "predicted_text": " \u0987\u09a8\u09cd\u09a6\u09c1\u0648\u0627\u062c",
63
+ "answer_logits": [
64
+ [
65
+ 0.6796875,
66
+ 0.66796875,
67
+ 0.6640625
68
+ ],
69
+ [
70
+ 0.71875,
71
+ 0.66015625,
72
+ 0.65625
73
+ ]
74
+ ]
75
+ },
76
+ "image": {
77
+ "predicted_tokens": [
78
+ "\u10d4\u10d1\u10e3\u10da\u10d8\u10d0"
79
+ ],
80
+ "predicted_text": "\u10d4\u10d1\u10e3\u10da\u10d8\u10d0",
81
+ "answer_logits": [
82
+ [
83
+ 0.70703125,
84
+ 0.703125,
85
+ 0.69921875
86
+ ]
87
+ ]
88
+ },
89
+ "text_image": {
90
+ "predicted_tokens": [
91
+ "\u10d4\u10d1\u10e3\u10da\u10d8\u10d0"
92
+ ],
93
+ "predicted_text": "\u10d4\u10d1\u10e3\u10da\u10d8\u10d0",
94
+ "answer_logits": [
95
+ [
96
+ 0.84765625,
97
+ 0.765625,
98
+ 0.66796875
99
+ ]
100
+ ]
101
+ }
102
+ },
103
  "training": {
104
  "loss": 12.520472526550293,
105
  "gradients": {
integrations/transformers/usage.md CHANGED
@@ -2,10 +2,14 @@
2
 
3
  This page is the Transformers contract of the Gemma3 MIC and also a usage example. [doc-builder](https://github.com/huggingface/doc-builder/blob/main/docs/runnable-code-blocks.md) renders the `runnable` blocks below as documentation, and its pytest plugin runs them as written against `google/gemma-3-4b-it`. The MIC runner runs the same blocks against each slim fixture and each checkpoint in [`integration.json`](../../integration.json). Lines marked `# doc-builder: hide` only matter there: they swap in the pinned checkpoint or variant and print the result record.
4
 
5
- ## Next-token prediction
 
 
6
 
7
  ```py runnable:gemma3
 
8
  import torch
 
9
  from transformers import AutoProcessor, AutoModelForCausalLM, set_seed
10
  import json # doc-builder: hide
11
  import os # doc-builder: hide
@@ -21,21 +25,38 @@ model = AutoModelForCausalLM.from_pretrained(model_id).to(device)
21
  if variant: # doc-builder: hide
22
  model = AutoModelForCausalLM.from_pretrained(model_id, **variant).to(device)
23
 
24
- prompts = ["The sky is", "Water is"]
25
- inputs = processor(text=prompts, return_tensors="pt", padding=True).to(device)
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  model.eval()
28
- with torch.no_grad():
29
- outputs = model(**inputs)
30
- logits = outputs.logits
31
-
32
- # Last-token logits for next-token prediction
33
- seq_lens = inputs["attention_mask"].sum(dim=-1) - 1
34
- last_logits = logits[torch.arange(inputs["input_ids"].shape[0], device=device), seq_lens]
35
- predicted_ids = last_logits.argmax(dim=-1)
36
- predicted_tokens = processor.tokenizer.convert_ids_to_tokens(predicted_ids)
37
- predicted_text = processor.tokenizer.decode(predicted_ids, skip_special_tokens=False)
38
- print(predicted_tokens)
 
 
 
 
 
39
  ```
40
 
41
  ## One training step
@@ -58,7 +79,6 @@ print(f"loss: {loss.item():.4f}")
58
  if os.environ.get("MIC_RUNNER"): # doc-builder: hide
59
  named = dict(model.named_parameters())
60
  checked = tuple(n for n, p in named.items() if p.grad is not None and p.ndim >= 2 and torch.count_nonzero(p.grad) > 0)[:2]
61
- top3 = last_logits.topk(3, dim=-1).values.float()
62
  result = {
63
  "runtime": {
64
  "device": next(model.parameters()).device.type,
@@ -66,9 +86,7 @@ if os.environ.get("MIC_RUNNER"): # doc-builder: hide
66
  "attn_implementation": model.config._attn_implementation,
67
  "attn_implementations": {n: getattr(model.config, n)._attn_implementation for n in sorted(model.config.sub_configs) if getattr(getattr(model.config, n, None), '_attn_implementation', None)},
68
  },
69
- "predicted_tokens": predicted_tokens,
70
- "predicted_text": predicted_text,
71
- "answer_logits": top3.tolist(),
72
  "training": {
73
  "loss": loss.item(),
74
  "gradients": {n: bool(named[n].grad is not None and torch.count_nonzero(named[n].grad) > 0 and torch.isfinite(named[n].grad).all()) for n in checked},
 
2
 
3
  This page is the Transformers contract of the Gemma3 MIC and also a usage example. [doc-builder](https://github.com/huggingface/doc-builder/blob/main/docs/runnable-code-blocks.md) renders the `runnable` blocks below as documentation, and its pytest plugin runs them as written against `google/gemma-3-4b-it`. The MIC runner runs the same blocks against each slim fixture and each checkpoint in [`integration.json`](../../integration.json). Lines marked `# doc-builder: hide` only matter there: they swap in the pinned checkpoint or variant and print the result record.
4
 
5
+ ## Next-token prediction, for every input case
6
+
7
+ Each input alone and all of them combined: text, image, text + image.
8
 
9
  ```py runnable:gemma3
10
+ import numpy as np
11
  import torch
12
+ from PIL import Image
13
  from transformers import AutoProcessor, AutoModelForCausalLM, set_seed
14
  import json # doc-builder: hide
15
  import os # doc-builder: hide
 
25
  if variant: # doc-builder: hide
26
  model = AutoModelForCausalLM.from_pretrained(model_id, **variant).to(device)
27
 
28
+ image = Image.fromarray(np.random.RandomState(0).randint(0, 255, (56, 56, 3), dtype=np.uint8))
29
+
30
+
31
+ def chat(*content):
32
+ return processor.apply_chat_template([{"role": "user", "content": list(content)}], tokenize=False, add_generation_prompt=True)
33
+
34
+
35
+ cases = {
36
+ "text": processor(text=["The sky is", "Water is"], return_tensors="pt", padding=True),
37
+ "image": processor(text=[chat({"type": "image"})], images=[image], return_tensors="pt"),
38
+ "text_image": processor(
39
+ text=[chat({"type": "image"}, {"type": "text", "text": "Describe this image."})], images=[image], return_tensors="pt"
40
+ ),
41
+ }
42
 
43
  model.eval()
44
+ predictions = {}
45
+ for name, inputs in cases.items():
46
+ inputs = inputs.to(device, dtype=model.dtype) # casts only floating tensors (pixel_values)
47
+ with torch.no_grad():
48
+ logits = model(**inputs).logits
49
+ # Last-token logits for next-token prediction
50
+ seq_lens = inputs["attention_mask"].sum(dim=-1) - 1
51
+ last_logits = logits[torch.arange(inputs["input_ids"].shape[0], device=device), seq_lens]
52
+ predicted_ids = last_logits.argmax(dim=-1)
53
+ predictions[name] = {
54
+ "predicted_tokens": processor.tokenizer.convert_ids_to_tokens(predicted_ids),
55
+ "predicted_text": processor.tokenizer.decode(predicted_ids, skip_special_tokens=False),
56
+ "answer_logits": last_logits.topk(3, dim=-1).values.float().tolist(),
57
+ }
58
+ print(name, predictions[name]["predicted_tokens"])
59
+ inputs = cases["text"].to(device) # the training step below uses the text case
60
  ```
61
 
62
  ## One training step
 
79
  if os.environ.get("MIC_RUNNER"): # doc-builder: hide
80
  named = dict(model.named_parameters())
81
  checked = tuple(n for n, p in named.items() if p.grad is not None and p.ndim >= 2 and torch.count_nonzero(p.grad) > 0)[:2]
 
82
  result = {
83
  "runtime": {
84
  "device": next(model.parameters()).device.type,
 
86
  "attn_implementation": model.config._attn_implementation,
87
  "attn_implementations": {n: getattr(model.config, n)._attn_implementation for n in sorted(model.config.sub_configs) if getattr(getattr(model.config, n, None), '_attn_implementation', None)},
88
  },
89
+ "cases": predictions,
 
 
90
  "training": {
91
  "loss": loss.item(),
92
  "gradients": {n: bool(named[n].grad is not None and torch.count_nonzero(named[n].grad) > 0 and torch.isfinite(named[n].grad).all()) for n in checked},