sat ( SwissArmyTransformer ) เป็นห้องสมุดที่ยืดหยุ่นและทรงพลังในการพัฒนาสายพันธุ์หม้อแปลงของคุณเอง
sat ได้รับการตั้งชื่อตาม "Swiss Army Knife" ซึ่งหมายความว่าทุกรุ่น (เช่น Bert, GPT, T5, GLM, Cogview, Vit ... ) แบ่งปันรหัสกระดูกสันหลังเดียวกัน และรองรับการใช้งานอเนกประสงค์ด้วยมิกซ์น้ำหนักเบาพิเศษ
sat ใช้พลังงานจาก deepspeed-ZeRO และ Model Parallelism โดยมีวัตถุประสงค์เพื่อให้แนวทางปฏิบัติที่ดีที่สุดสำหรับการเตรียมการและการปรับรูปแบบขนาดใหญ่ (พารามิเตอร์ 100m ~ 20b)
pip install SwissArmyTransformer
เพิ่มส่วนประกอบโมเดล-ไม่เชื่อเรื่องพระเจ้า เช่นการปรับแต่งคำนำหน้าใน หนึ่ง บรรทัด!
class ClassificationModel ( GLMModel ): # can also be BertModel, RobertaModel, etc.
def __init__ ( self , args , transformer = None , ** kwargs ):
super (). __init__ ( args , transformer = transformer , ** kwargs )
self . add_mixin ( 'classification_head' , MLPHeadMixin ( args . hidden_size , 2048 , 1 ))
# Arm an arbitrary model with Prefix-tuning with this line!
self . add_mixin ( 'prefix-tuning' , PrefixTuningMixin ( args . num_layers , args . hidden_size // args . num_attention_heads , args . num_attention_heads , args . prefix_len )) model , args = AutoModel . from_pretrained ( 'glm-10b-chinese' , args )
model . add_mixin ( 'auto-regressive' , CachedAutoregressiveMixin ())
# Generate a sequence with beam search
from sat . generation . autoregressive_sampling import filling_sequence
from sat . generation . sampling_strategies import BeamSearchStrategy
output , * mems = filling_sequence ( model , input_seq ,
batch_size = args . batch_size ,
strategy = BeamSearchStrategy ( args . batch_size ))สร้างโมเดลที่ใช้หม้อแปลงด้วยรหัสน้อยที่สุด เราพูดถึง GLM ซึ่งแตกต่างจากหม้อแปลงมาตรฐาน (เรียกว่า basemodel) ในการฝังตำแหน่ง (และการสูญเสียการฝึกอบรม) เราต้องมุ่งเน้นไปที่ส่วนที่เกี่ยวข้องเมื่อเข้ารหัส
class BlockPositionEmbeddingMixin ( BaseMixin ):
# Here define parameters for the mixin
def __init__ ( self , max_sequence_length , hidden_size , init_method_std = 0.02 ):
super ( BlockPositionEmbeddingMixin , self ). __init__ ()
self . max_sequence_length = max_sequence_length
self . hidden_size = hidden_size
self . block_position_embeddings = torch . nn . Embedding ( max_sequence_length , hidden_size )
torch . nn . init . normal_ ( self . block_position_embeddings . weight , mean = 0.0 , std = init_method_std )
# Here define the method for the mixin
def position_embedding_forward ( self , position_ids , ** kwargs ):
position_ids , block_position_ids = position_ids [:, 0 ], position_ids [:, 1 ]
position_embeddings = self . transformer . position_embeddings ( position_ids )
block_position_embeddings = self . block_position_embeddings ( block_position_ids )
return position_embeddings + block_position_embeddings
class GLMModel ( BaseModel ):
def __init__ ( self , args , transformer = None ):
super (). __init__ ( args , transformer = transformer )
self . add_mixin ( 'block_position_embedding' ,
BlockPositionEmbeddingMixin ( args . max_sequence_length , args . hidden_size )
) # Add the mixin for GLM การสนับสนุนที่ครอบคลุมสำหรับการฝึกอบรม sat มีจุดมุ่งหมายเพื่อให้แนวทางปฏิบัติที่ดีที่สุดสำหรับการเตรียมการและการปรับแต่งซึ่งคุณต้องทำไป forward_step และ create_dataset_function แต่ด้วยพารามิเตอร์ hyperparameters เพื่อเปลี่ยนการกำหนดค่าการฝึกอบรมที่มีประโยชน์
--num_nodes , --num_gpus และ hostfile อย่างง่ายmemmap ไฟล์ Python ทั่วไปที่ใช้ Bert ใน SAT (สำหรับการอนุมาน) มีดังนี้:
# @File: inference_bert.py
from sat import get_args , get_tokenizer , AutoModel
# Parse args, initialize the environment. This is necessary.
args = get_args ()
# Automatically download and load model. Will also dump model-related hyperparameters to args.
model , args = AutoModel . from_pretrained ( 'bert-base-uncased' , args )
# Get the BertTokenizer according to args.tokenizer_type (automatically set).
tokenizer = get_tokenizer ( args )
# Here to use bert as you want!
# ...จากนั้นเราสามารถเรียกใช้รหัสผ่าน
SAT_HOME=/path/to/download python inference_bert.py --mode inferenceชื่อโมเดลที่ได้รับการสนับสนุนอย่างเป็นทางการทั้งหมดอยู่ใน URLS.py
ในการปรับหรือทำให้หม้อแปลงเป็นเรื่องง่ายมากเช่นกัน!
# @File: finetune_bert.py
from sat import get_args , get_tokenizer , AutoModel
from sat . model . mixins import MLPHeadMixin
def create_dataset_function ( path , args ):
# Here to load the dataset
# ...
assert isinstance ( dataset , torch . utils . data . Dataset )
return dataset
def forward_step ( data_iterator , model , args , timers ):
inputs = next ( data_iterator ) # from the dataset of create_dataset_function.
loss , * others = model ( inputs )
return loss
# Parse args, initialize the environment. This is necessary.
args = get_args ()
model , args = AutoModel . from_pretrained ( 'bert-base-uncased' , args )
tokenizer = get_tokenizer ( args )
# Here to use bert as you want!
model . del_mixin ( 'bert-final' )
model . add_mixin ( 'classification_head' , MLPHeadMixin ( args . hidden_size , 2048 , 1 ))
# ONE LINE to train!
# args already includes hyperparams such as lr, train-iters, zero-stage ...
training_main ( args ,
model_cls = model ,
forward_step_function = forward_step , # user define
create_dataset_function = create_dataset_function # user define
)จากนั้นเราสามารถเรียกใช้รหัสผ่าน
deepspeed --include localhost:0,1 finetune_bert.py
--experiment-name ftbert
--mode finetune --train-iters 1000 --save /path/to/save
--train-data /path/to/train --valid-data /path/to/valid
--lr 0.00002 --batch-size 8 --zero-stage 1 --fp16 ที่นี่เราใช้ข้อมูลขนานบน GPU 0,1 นอกจากนี้เรายังสามารถเปิดตัวการฝึกอบรมเกี่ยวกับเครื่องจักรที่เชื่อมต่อระหว่างกันหลายเครื่องผ่าน --hostfile /path/to/hostfile ดูการสอนสำหรับรายละเอียดเพิ่มเติม
ในการเขียนแบบจำลองของคุณเองคุณจะต้องพิจารณาความแตกต่างระหว่างหม้อแปลงมาตรฐาน ตัวอย่างเช่นหากคุณมีความคิดที่จะปรับปรุงการดำเนินงานความสนใจ:
from sat . model import BaseMixin
class MyAttention ( BaseMixin ):
def __init__ ( self , hidden_size ):
super ( MyAttention , self ). __init__ ()
# MyAttention may needs some new params, e.g. a learnable alpha.
self . learnable_alpha = torch . nn . Parameter ( torch . ones ( hidden_size ))
# This is a hook function, the name `attention_fn` is special.
def attention_fn ( q , k , v , mask , dropout = None , ** kwargs ):
# Code for my attention.
# ...
return attention_results ที่นี่ attention_fn เป็นฟังก์ชันเบ็ดแทนที่การกระทำเริ่มต้นด้วยฟังก์ชั่นใหม่ ตะขอที่มีอยู่ทั้งหมดอยู่ใน transformer_defaults.py ตอนนี้เราสามารถใช้ add_mixin เพื่อใช้การเปลี่ยนแปลงของเรากับหม้อแปลงทั้งหมดเช่น Bert, Vit และ Cogview ดูการสอนสำหรับรายละเอียดเพิ่มเติม
ขณะนี้เราไม่มีกระดาษดังนั้นคุณไม่จำเป็นต้องอ้างอิงเราอย่างเป็นทางการ! ~
หากโครงการนี้ช่วยงานวิจัยหรือวิศวกรรมของคุณให้ใช้ footnote{https://github.com/THUDM/SwissArmyTransformer} เพื่อพูดถึงเราและแนะนำ SwissArmyTransformer ต่อผู้อื่น
การสอนสำหรับการมีส่วนร่วม SAT กำลังมา!
โครงการขึ้นอยู่กับ (ผู้ใช้) Deepspeed, Megatron-LM และ HuggingFace Transformers ขอบคุณสำหรับงานที่ยอดเยี่ยมของพวกเขา