OpenDFM/SciDFM-MoE-A5.6B-v1.0

Model

SciDFM: Dialogue Foundation Model for Science

2

11 commits

1 linked in READMEs

updated Nov 5, 2024

See the code

README

SciDFM: Dialogue Foundation Model for Science

SciDFM is the pioneering open-sourced dialogue foundation model tailored for science, which integrates a mixture-of-experts architecture into a transformer-based framework, aiming at enhancing its sophisticated scientific reasoning and understanding capabilities. SciDFM achieves strong performance on general scientific benchmarks such as SciEval and SciQ, and it reachs a SOTA performance on domain-specific benchmark among models of similar size.

News

  • 2024-06-28 The parameter of SciDFM-MoE-A5.6B-v1.0 is open-soursed! Technical report is coming soon.

Model Details

SciDFM is based on a transformer architecture, and follows modifications of Llama, i.e. RMSNorm, RoPE and SwiGLU. SciDFM use the same hyper-parameters of OpenLLaMa-3B. And in order to better model knowledge of different disciplines, we replace the feed-forward block with Mixture-of-Expert (MoE) layers.

Training Details

SciDFM is pre-trained on a large corpus containing ~300B science tokens and ~270B general tokens for two epochs, resulting in about 1.1T tokens consuming. And we further fine-tune SciDFM using ~9.3M instruction-following samples for 5 epochs to improve the performances on the downstream benchmarks.

Usage Details

Local Inference

To load and run SciDFM locally, here is an example:

import torch
from transformers import LlamaTokenizer, AutoModelForCausalLM

model_name_or_id = "OpenDFM/SciDFM-MoE-A5.6B-v1.0"
tokenizer = LlamaTokenizer.from_pretrained(model_name_or_id, use_fast=False)
model = AutoModelForCausalLM.from_pretrained(model_name_or_id, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True)

chat_template = "<|user|>:{instruction}<|assistant|>:"
input_text = "What is Mixture-of-Experts (MoE) in computer science?"
input_text = chat_template.format(instruction=input_text)

inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
generation_config = GenerationConfig(
    do_sample=True,
    top_k=20,
    top_p=0.9,
    temperature=0.9,
    max_new_tokens=1024,
    eos_token_id=tokenizer.eos_token_id
)

outputs = model.generate(**inputs, generation_config=generation_config)
generated_text = tokenizer.decode(outputs, skip_special_tokens=True)[0][len(input_text):]
print(generated_text.strip())

SMILES preprocess

When there involves SMILES notation in your input, we recommend to preprocess the SMILES with the rdkit package to canonicalize the SMILES. Here is an example:

from rdkit import Chem
def canonicalize_smiles(smiles):
    mol = Chem.MolFromSmiles(smiles)
    if mol is None:
        return None
    return Chem.MolToSmiles(mol, isomericSmiles=True, kekuleSmiles=False)

or directly:

from rdkit import Chem
def canonicalize_smiles(smiles):
    return Chem.CanonSmiles(smiles, useChiral=True)

Special Tokens preprocess

If there is SMILES expression in your input, please first process it with the following function:

import sentencepiece as spm

smiles_model = spm.SentencePieceProcessor(model_file="smiles.model")

def convert_smiles(smiles_str):
   pieces = smiles_model.encode_as_pieces(smiles_str)[1:]
   smiles = "".join([f"[ChemDFM_Start_SMILES_Unit]{piece}[ChemDFM_End_SMILES_Unit]" for piece in pieces])
   return smiles

convert_smiles("C(C(=O)O)N")

And if there is protein sequece in your input, please first process it with the following function:

def convert_protein(p_str):
   res = [f"<<protein>>{s}" for s in p_str]
   return "".join(res)

convert_protein("MIRLGAPQTL")

Evaluation

We briefly compare SciDFM-MoE-A5.6B-v1.0 with similar-sized instruction-tuned LLMs on scientific evaluation benchmarks. The results are shown below:

ModelSciEvalSciQARC_cARC_eGSM8KMATHMedQAMMCQAPMQAAvg
LLaMa2-7B27.0657.0036.4346.593.943.9626.3229.8466.8032.95
Galactica-6.7B46.2874.2044.2861.832.806.3230.4836.4648.8038.91
LLaMa2-13B33.8878.1056.6672.3522.823.9032.6834.2877.8045.45
ChatGLM2-6B54.2575.8057.0873.5725.097.1827.4234.2160.4045.94
Galactica-30B54.2483.1057.8575.0413.658.6637.7148.4358.8048.35
LLaMa3-8B59.7090.0071.1684.055.917.0048.7852.7426.6049.59
ChatGLM3-6B51.1377.6060.8475.9760.2723.5224.5931.3951.8050.53
SciGLM-6B61.2288.7077.4786.5742.2316.4042.8144.9473.6059.12
SciDFM62.4888.0064.7681.4859.1427.2844.5453.1078.0061.56
ChatGLM3-6B-base60.3489.0078.5887.3759.8222.6442.7345.1474.4061.96
Llama3-8B-Instruct64.9191.6076.4587.3376.5726.2656.4859.3172.0067.44

Citation

@article{sun2024scidfm,
  title={SciDFM: A Large Language Model with Mixture-of-Experts for Science},
  author={Sun, Liangtai and Luo, Danyu and Ma, Da and Zhao, Zihan and Chen, Baocai and Shen, Zhennan and Zhu, Su and Chen, Lu and Chen, Xin and Yu, Kai},
  journal={arXiv preprint arXiv:2409.18412},
  year={2024}
}
AI4S
custom_code
safetensors
SciDFM
text-generation
transformers

Contributors

Liangtai

6 commits

LS
Liangtai Sun

5 commits

OpenDFM/SciDFM-MoE-A5.6B-v1.0

Model

SciDFM: Dialogue Foundation Model for Science

2

11 commits

1 linked in READMEs

updated Nov 5, 2024

See the code

README

SciDFM: Dialogue Foundation Model for Science

SciDFM is the pioneering open-sourced dialogue foundation model tailored for science, which integrates a mixture-of-experts architecture into a transformer-based framework, aiming at enhancing its sophisticated scientific reasoning and understanding capabilities. SciDFM achieves strong performance on general scientific benchmarks such as SciEval and SciQ, and it reachs a SOTA performance on domain-specific benchmark among models of similar size.

News

  • 2024-06-28 The parameter of SciDFM-MoE-A5.6B-v1.0 is open-soursed! Technical report is coming soon.

Model Details

SciDFM is based on a transformer architecture, and follows modifications of Llama, i.e. RMSNorm, RoPE and SwiGLU. SciDFM use the same hyper-parameters of OpenLLaMa-3B. And in order to better model knowledge of different disciplines, we replace the feed-forward block with Mixture-of-Expert (MoE) layers.

Training Details

SciDFM is pre-trained on a large corpus containing ~300B science tokens and ~270B general tokens for two epochs, resulting in about 1.1T tokens consuming. And we further fine-tune SciDFM using ~9.3M instruction-following samples for 5 epochs to improve the performances on the downstream benchmarks.

Usage Details

Local Inference

To load and run SciDFM locally, here is an example:

import torch
from transformers import LlamaTokenizer, AutoModelForCausalLM

model_name_or_id = "OpenDFM/SciDFM-MoE-A5.6B-v1.0"
tokenizer = LlamaTokenizer.from_pretrained(model_name_or_id, use_fast=False)
model = AutoModelForCausalLM.from_pretrained(model_name_or_id, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True)

chat_template = "<|user|>:{instruction}<|assistant|>:"
input_text = "What is Mixture-of-Experts (MoE) in computer science?"
input_text = chat_template.format(instruction=input_text)

inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
generation_config = GenerationConfig(
    do_sample=True,
    top_k=20,
    top_p=0.9,
    temperature=0.9,
    max_new_tokens=1024,
    eos_token_id=tokenizer.eos_token_id
)

outputs = model.generate(**inputs, generation_config=generation_config)
generated_text = tokenizer.decode(outputs, skip_special_tokens=True)[0][len(input_text):]
print(generated_text.strip())

SMILES preprocess

When there involves SMILES notation in your input, we recommend to preprocess the SMILES with the rdkit package to canonicalize the SMILES. Here is an example:

from rdkit import Chem
def canonicalize_smiles(smiles):
    mol = Chem.MolFromSmiles(smiles)
    if mol is None:
        return None
    return Chem.MolToSmiles(mol, isomericSmiles=True, kekuleSmiles=False)

or directly:

from rdkit import Chem
def canonicalize_smiles(smiles):
    return Chem.CanonSmiles(smiles, useChiral=True)

Special Tokens preprocess

If there is SMILES expression in your input, please first process it with the following function:

import sentencepiece as spm

smiles_model = spm.SentencePieceProcessor(model_file="smiles.model")

def convert_smiles(smiles_str):
   pieces = smiles_model.encode_as_pieces(smiles_str)[1:]
   smiles = "".join([f"[ChemDFM_Start_SMILES_Unit]{piece}[ChemDFM_End_SMILES_Unit]" for piece in pieces])
   return smiles

convert_smiles("C(C(=O)O)N")

And if there is protein sequece in your input, please first process it with the following function:

def convert_protein(p_str):
   res = [f"<<protein>>{s}" for s in p_str]
   return "".join(res)

convert_protein("MIRLGAPQTL")

Evaluation

We briefly compare SciDFM-MoE-A5.6B-v1.0 with similar-sized instruction-tuned LLMs on scientific evaluation benchmarks. The results are shown below:

ModelSciEvalSciQARC_cARC_eGSM8KMATHMedQAMMCQAPMQAAvg
LLaMa2-7B27.0657.0036.4346.593.943.9626.3229.8466.8032.95
Galactica-6.7B46.2874.2044.2861.832.806.3230.4836.4648.8038.91
LLaMa2-13B33.8878.1056.6672.3522.823.9032.6834.2877.8045.45
ChatGLM2-6B54.2575.8057.0873.5725.097.1827.4234.2160.4045.94
Galactica-30B54.2483.1057.8575.0413.658.6637.7148.4358.8048.35
LLaMa3-8B59.7090.0071.1684.055.917.0048.7852.7426.6049.59
ChatGLM3-6B51.1377.6060.8475.9760.2723.5224.5931.3951.8050.53
SciGLM-6B61.2288.7077.4786.5742.2316.4042.8144.9473.6059.12
SciDFM62.4888.0064.7681.4859.1427.2844.5453.1078.0061.56
ChatGLM3-6B-base60.3489.0078.5887.3759.8222.6442.7345.1474.4061.96
Llama3-8B-Instruct64.9191.6076.4587.3376.5726.2656.4859.3172.0067.44

Citation

@article{sun2024scidfm,
  title={SciDFM: A Large Language Model with Mixture-of-Experts for Science},
  author={Sun, Liangtai and Luo, Danyu and Ma, Da and Zhao, Zihan and Chen, Baocai and Shen, Zhennan and Zhu, Su and Chen, Lu and Chen, Xin and Yu, Kai},
  journal={arXiv preprint arXiv:2409.18412},
  year={2024}
}
AI4S
custom_code
safetensors
SciDFM
text-generation
transformers

Contributors

Liangtai

6 commits

LS
Liangtai Sun

5 commits