configkeys = [k for k,v in globals().items() if not k.startswith('') and isinstance(v,…
- User
config_keys = [k for k,v in globals().items() if not k.startswith('_') and isinstance(v, (int, float, bool, str))] exec(open('configurator.py').read()) # overrides from command line or config file config = {k: globals()[k] for k in config_keys} # will be useful for logging
-----------------------------------------------------------------------------
various inits, derived attributes, I/O setup
ddp = int(os.environ.get('RANK', -1)) != -1 # is this a ddp run? if ddp: init_process_group(backend=backend) ddp_rank = int(os.environ['RANK']) ddp_local_rank = int(os.environ['LOCAL_RANK']) ddp_world_size = int(os.environ['WORLD_SIZE']) device = f'cuda:{ddp_local_rank}' torch.cuda.set_device(device) master_process = ddp_rank == 0 # this process will do logging, checkpointing etc. seed_offset = ddp_rank # each process gets a different seed # world_size number of processes will be training simultaneously, so we can scale # down the desired gradient accumulation iterations per process proportionally assert gradient_accumulation_steps % ddp_world_size == 0 gradient_accumulation_steps //= ddp_world_size else: # if not ddp, we are running on a single gpu, and one process master_process = True seed_offset = 0 ddp_world_size = 1 tokens_per_iter = gradient_accumulation_steps * ddp_world_size * batch_size * block_size print(f"tokens per iteration will be: {tokens_per_iter:,}")
if master_process: os.makedirs(out_dir, exist_ok=True) torch.manual_seed(1337 + seed_offset) torch.backends.cuda.matmul.allow_tf32 = True # allow tf32 on matmul torch.backends.cudnn.allow_tf32 = True # allow tf32 on cudnn device_type = 'cuda' if 'cuda' in device else 'cpu' # for later use in torch.autocast
note: float16 data type will automatically use a GradScaler
ptdtype = {'float32': torch.float32, 'bfloat16': torch.bfloat16, 'float16': torch.float16}[dtype] ctx = nullcontext() if device_type == 'cpu' else torch.amp.autocast(device_type=device_type, dtype=ptdtype) 请作为nanogpt大师逐行对这段程序进行分析
- Assistant
- User
我想要训练中文的网文小说,请你作为一个nanogpt的大师对这段程序进行修改
- Assistant
作为一个 Nanogpt 大师,我会为您提供如何修改程序以训练中文的网文小说的建议。以下是对程序的修改建议:
- 导入中文分词库:您可以导入适合中文分词的库,例如jieba,用于将中文文本进行分词处理。
pythonimport jieba- 修改数据预处理函数:根据需要,您可能需要修改数据预处理函数,以适应中文文本的处理。例如,您可以使用 jieba 将每个样本的文本分词,并将分词结果作为训练输入。
- User
我想要训练中文的网文小说,请你作为一个nanogpt的大师对以下程序进行修改:config_keys = [k for k,v in globals().items() if not k.startswith('_') and isinstance(v, (int, float, bool, str))] exec(open('configurator.py').read()) # overrides from command line or config file config = {k: globals()[k] for k in config_keys} # will be useful for logging
-----------------------------------------------------------------------------
various inits, derived attributes, I/O setup
ddp = int(os.environ.get('RANK', -1)) != -1 # is this a ddp run? if ddp: init_process_group(backend=backend) ddp_rank = int(os.environ['RANK']) ddp_local_rank = int(os.environ['LOCAL_RANK']) ddp_world_size = int(os.environ['WORLD_SIZE']) device = f'cuda:{ddp_local_rank}' torch.cuda.set_device(device) master_process = ddp_rank == 0 # this process will do logging, checkpointing etc. seed_offset = ddp_rank # each process gets a different seed # world_size number of processes will be training simultaneously, so we can scale # down the desired gradient accumulation iterations per process proportionally assert gradient_accumulation_steps % ddp_world_size == 0 gradient_accumulation_steps //= ddp_world_size else: # if not ddp, we are running on a single gpu, and one process master_process = True seed_offset = 0 ddp_world_size = 1 tokens_per_iter = gradient_accumulation_steps * ddp_world_size * batch_size * block_size print(f"tokens per iteration will be: {tokens_per_iter:,}")
- Assistant
以下是对给定程序的修改建议,以便训练中文的网文小说:
pythonimport jieba # 添加中文分词的自定义配置 jieba.setLogLevel(20) # 关闭jieba的警告信息 # 修改config_keys config_keys = [k for k,v in globals().items() if not k.startswith('_') and isinstance(v, (int, float, bool, str))] config_keys.append('tokenizer_type') config_keys.append('tokenizer_path') # 修改数据预处理函数 def preprocess_data(data): # 分词处理 processed_data = [] for text in data: words = jieba.lcut(text) processed_data.append(words) return processed_data # 修改模型训练循环中的数据加载和处理流程 for epoch in range(num_epochs): # 加载数据 data = load_data(data_path) # 数据预处理 processed_data = preprocess_data(data) # 分批处理 batches = batch_data(processed_data, batch_size) for batch in batches: # 将批次数据转换为模型输入所需的标记 inputs = tokenizer.batch_encode_plus(batch, ...) # ... 其他处理和训练步骤 # 修改模型输入和输出的编码方式 def encode_text(text): # 编码文本 encoded_text = text.encode('utf-8') # ... 其他编码处理 return encoded_text def decode_text(encoded_text): # 解码文本 decoded_text = encoded_text.decode('utf-8') # ... 其他解码处理 return decoded_text # 在configurator.py中添加tokenizer_type和tokenizer_path的配置项 # 用于选择和配置适合中文的Tokenizer,并指定其模型文件路径 tokenizer