Skip to content

Commit 06a84b5

Browse files
authored
Add Node.js support for Gemma 3 (text) (#1229)
* Add support for gemma 3 (text) * Fix tensor slicing past boundaries
1 parent 566ef11 commit 06a84b5

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ You can refine your search by selecting the task you're interested in (e.g., [te
330330
1. **Florence2** (from Microsoft) released with the paper [Florence-2: Advancing a Unified Representation for a Variety of Vision Tasks](https://arxiv.org/abs/2311.06242) by Bin Xiao, Haiping Wu, Weijian Xu, Xiyang Dai, Houdong Hu, Yumao Lu, Michael Zeng, Ce Liu, Lu Yuan.
331331
1. **[Gemma](https://huggingface.co/docs/transformers/main/model_doc/gemma)** (from Google) released with the paper [Gemma: Open Models Based on Gemini Technology and Research](https://blog.google/technology/developers/gemma-open-models/) by the Gemma Google team.
332332
1. **[Gemma2](https://huggingface.co/docs/transformers/main/model_doc/gemma2)** (from Google) released with the paper [Gemma2: Open Models Based on Gemini Technology and Research](https://blog.google/technology/developers/google-gemma-2/) by the Gemma Google team.
333+
1. **[Gemma3](https://huggingface.co/docs/transformers/main/model_doc/gemma3)** (from Google) released with the paper [Introducing Gemma 3: The most capable model you can run on a single GPU or TPU](https://blog.google/technology/developers/gemma-3/) by the Gemma Google team.
333334
1. **[GLM](https://huggingface.co/docs/transformers/main/model_doc/glm)** (from the GLM Team, THUDM & ZhipuAI) released with the paper [ChatGLM: A Family of Large Language Models from GLM-130B to GLM-4 All Tools](https://arxiv.org/abs/2406.12793v2) by Team GLM: Aohan Zeng, Bin Xu, Bowen Wang, Chenhui Zhang, Da Yin, Dan Zhang, Diego Rojas, Guanyu Feng, Hanlin Zhao, Hanyu Lai, Hao Yu, Hongning Wang, Jiadai Sun, Jiajie Zhang, Jiale Cheng, Jiayi Gui, Jie Tang, Jing Zhang, Jingyu Sun, Juanzi Li, Lei Zhao, Lindong Wu, Lucen Zhong, Mingdao Liu, Minlie Huang, Peng Zhang, Qinkai Zheng, Rui Lu, Shuaiqi Duan, Shudan Zhang, Shulin Cao, Shuxun Yang, Weng Lam Tam, Wenyi Zhao, Xiao Liu, Xiao Xia, Xiaohan Zhang, Xiaotao Gu, Xin Lv, Xinghan Liu, Xinyi Liu, Xinyue Yang, Xixuan Song, Xunkai Zhang, Yifan An, Yifan Xu, Yilin Niu, Yuantao Yang, Yueyan Li, Yushi Bai, Yuxiao Dong, Zehan Qi, Zhaoyu Wang, Zhen Yang, Zhengxiao Du, Zhenyu Hou, Zihan Wang.
334335
1. **[GLPN](https://huggingface.co/docs/transformers/model_doc/glpn)** (from KAIST) released with the paper [Global-Local Path Networks for Monocular Depth Estimation with Vertical CutDepth](https://arxiv.org/abs/2201.07436) by Doyeon Kim, Woonghyun Ga, Pyungwhan Ahn, Donggyu Joo, Sehwan Chun, Junmo Kim.
335336
1. **[GPT Neo](https://huggingface.co/docs/transformers/model_doc/gpt_neo)** (from EleutherAI) released in the repository [EleutherAI/gpt-neo](https://github.com/EleutherAI/gpt-neo) by Sid Black, Stella Biderman, Leo Gao, Phil Wang and Connor Leahy.

docs/snippets/6_supported-models.snippet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
1. **Florence2** (from Microsoft) released with the paper [Florence-2: Advancing a Unified Representation for a Variety of Vision Tasks](https://arxiv.org/abs/2311.06242) by Bin Xiao, Haiping Wu, Weijian Xu, Xiyang Dai, Houdong Hu, Yumao Lu, Michael Zeng, Ce Liu, Lu Yuan.
4545
1. **[Gemma](https://huggingface.co/docs/transformers/main/model_doc/gemma)** (from Google) released with the paper [Gemma: Open Models Based on Gemini Technology and Research](https://blog.google/technology/developers/gemma-open-models/) by the Gemma Google team.
4646
1. **[Gemma2](https://huggingface.co/docs/transformers/main/model_doc/gemma2)** (from Google) released with the paper [Gemma2: Open Models Based on Gemini Technology and Research](https://blog.google/technology/developers/google-gemma-2/) by the Gemma Google team.
47+
1. **[Gemma3](https://huggingface.co/docs/transformers/main/model_doc/gemma3)** (from Google) released with the paper [Introducing Gemma 3: The most capable model you can run on a single GPU or TPU](https://blog.google/technology/developers/gemma-3/) by the Gemma Google team.
4748
1. **[GLM](https://huggingface.co/docs/transformers/main/model_doc/glm)** (from the GLM Team, THUDM & ZhipuAI) released with the paper [ChatGLM: A Family of Large Language Models from GLM-130B to GLM-4 All Tools](https://arxiv.org/abs/2406.12793v2) by Team GLM: Aohan Zeng, Bin Xu, Bowen Wang, Chenhui Zhang, Da Yin, Dan Zhang, Diego Rojas, Guanyu Feng, Hanlin Zhao, Hanyu Lai, Hao Yu, Hongning Wang, Jiadai Sun, Jiajie Zhang, Jiale Cheng, Jiayi Gui, Jie Tang, Jing Zhang, Jingyu Sun, Juanzi Li, Lei Zhao, Lindong Wu, Lucen Zhong, Mingdao Liu, Minlie Huang, Peng Zhang, Qinkai Zheng, Rui Lu, Shuaiqi Duan, Shudan Zhang, Shulin Cao, Shuxun Yang, Weng Lam Tam, Wenyi Zhao, Xiao Liu, Xiao Xia, Xiaohan Zhang, Xiaotao Gu, Xin Lv, Xinghan Liu, Xinyi Liu, Xinyue Yang, Xixuan Song, Xunkai Zhang, Yifan An, Yifan Xu, Yilin Niu, Yuantao Yang, Yueyan Li, Yushi Bai, Yuxiao Dong, Zehan Qi, Zhaoyu Wang, Zhen Yang, Zhengxiao Du, Zhenyu Hou, Zihan Wang.
4849
1. **[GLPN](https://huggingface.co/docs/transformers/model_doc/glpn)** (from KAIST) released with the paper [Global-Local Path Networks for Monocular Depth Estimation with Vertical CutDepth](https://arxiv.org/abs/2201.07436) by Doyeon Kim, Woonghyun Ga, Pyungwhan Ahn, Donggyu Joo, Sehwan Chun, Junmo Kim.
4950
1. **[GPT Neo](https://huggingface.co/docs/transformers/model_doc/gpt_neo)** (from EleutherAI) released in the repository [EleutherAI/gpt-neo](https://github.com/EleutherAI/gpt-neo) by Sid Black, Stella Biderman, Leo Gao, Phil Wang and Connor Leahy.

src/configs.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function getNormalizedConfig(config) {
6767
// Sub-configs
6868
case 'llava':
6969
case 'paligemma':
70+
case 'gemma3':
7071
case 'florence2':
7172
case 'llava_onevision':
7273
case 'idefics3':
@@ -126,6 +127,7 @@ function getNormalizedConfig(config) {
126127
break;
127128
case 'gemma':
128129
case 'gemma2':
130+
case 'gemma3_text':
129131
case 'glm':
130132
case 'helium':
131133
mapping['num_heads'] = 'num_key_value_heads';

src/models.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,8 @@ async function decoderForward(self, model_inputs, is_encoder_decoder = false) {
594594
new_model_inputs.use_cache_branch = boolTensor(!!past_key_values);
595595
}
596596
if (session.inputNames.includes('position_ids') && new_model_inputs.attention_mask && !new_model_inputs.position_ids) {
597-
// NOTE: Handle a special case for paligemma models, where positions are 1-indexed
598-
const start_index = self.config.model_type === 'paligemma' ? 1 : 0;
597+
// NOTE: Handle a special case for paligemma/gemma3 models, where positions are 1-indexed
598+
const start_index = ['paligemma', 'gemma3_text', 'gemma3'].includes(self.config.model_type) ? 1 : 0;
599599
new_model_inputs.position_ids = createPositionIds(new_model_inputs, past_key_values, start_index);
600600
}
601601

@@ -4520,6 +4520,23 @@ export class Gemma2Model extends Gemma2PreTrainedModel { }
45204520
export class Gemma2ForCausalLM extends Gemma2PreTrainedModel { }
45214521
//////////////////////////////////////////////////
45224522

4523+
4524+
//////////////////////////////////////////////////
4525+
// Gemma3 models
4526+
4527+
/**
4528+
* The bare Gemma3 Model outputting raw hidden-states without any specific head on top.
4529+
*/
4530+
export class Gemma3PreTrainedModel extends PreTrainedModel { }
4531+
/**
4532+
* The bare Gemma3 Model outputting raw hidden-states without any specific head on top.
4533+
*/
4534+
export class Gemma3Model extends Gemma3PreTrainedModel { }
4535+
4536+
export class Gemma3ForCausalLM extends Gemma3PreTrainedModel { }
4537+
//////////////////////////////////////////////////
4538+
4539+
45234540
//////////////////////////////////////////////////
45244541
export class OpenELMPreTrainedModel extends PreTrainedModel { }
45254542
export class OpenELMModel extends OpenELMPreTrainedModel { }
@@ -7553,6 +7570,7 @@ const MODEL_MAPPING_NAMES_DECODER_ONLY = new Map([
75537570
['cohere', ['CohereModel', CohereModel]],
75547571
['gemma', ['GemmaModel', GemmaModel]],
75557572
['gemma2', ['Gemma2Model', Gemma2Model]],
7573+
['gemma3_text', ['Gemma3Model', Gemma3Model]],
75567574
['helium', ['HeliumModel', HeliumModel]],
75577575
['glm', ['GlmModel', GlmModel]],
75587576
['openelm', ['OpenELMModel', OpenELMModel]],
@@ -7652,6 +7670,7 @@ const MODEL_FOR_CAUSAL_LM_MAPPING_NAMES = new Map([
76527670
['cohere', ['CohereForCausalLM', CohereForCausalLM]],
76537671
['gemma', ['GemmaForCausalLM', GemmaForCausalLM]],
76547672
['gemma2', ['Gemma2ForCausalLM', Gemma2ForCausalLM]],
7673+
['gemma3_text', ['Gemma3ForCausalLM', Gemma3ForCausalLM]],
76557674
['helium', ['HeliumForCausalLM', HeliumForCausalLM]],
76567675
['glm', ['GlmForCausalLM', GlmForCausalLM]],
76577676
['openelm', ['OpenELMForCausalLM', OpenELMForCausalLM]],

src/utils/tensor.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,8 +1182,12 @@ function calc_unsqueeze_dims(dims, dim) {
11821182
* @private
11831183
*/
11841184
function safeIndex(index, size, dimension = null, boundsCheck = true) {
1185-
if (boundsCheck && (index < -size || index >= size)) {
1186-
throw new Error(`IndexError: index ${index} is out of bounds for dimension${dimension === null ? '' : ' ' + dimension} with size ${size}`);
1185+
if (index < -size || index >= size) {
1186+
if (boundsCheck) {
1187+
throw new Error(`IndexError: index ${index} is out of bounds for dimension${dimension === null ? '' : ' ' + dimension} with size ${size}`);
1188+
} else {
1189+
return index < -size ? 0 : size;
1190+
}
11871191
}
11881192

11891193
if (index < 0) {

0 commit comments

Comments
 (0)