"""FP32 master weights -> the BF16 model folder. Every tensor is rounded FP32 -> BF16, to nearest with ties to even (fp32_common.py states the rule), except the 48 linear-attention decay tensors (...linear_attn.A_log), which stay FP32 as they are in the BF16 release. The vision tower, which was never trained here, is an exact FP32 copy of its BF16 values, so rounding returns them. Tensor names, the split into weight files and the index are those of the BF16 release; config, tokenizer and chat template are copied. The output carries its own SHA256SUMS. usage: masters_to_bf16.py SRC DST [--check] [--no-mtp] [--mtp FILE] [--sums FILE] [--check-only] [--workers 8] [--threads 4] [--exact-rounding] SRC the FP32 master-weights folder (the release folder) DST the BF16 folder to write (must not exist) --mtp FILE the draft head's FP32 weights (default SRC/mtp.safetensors). It is written into the weight files the BF16 release has it in, so the output equals the full BF16 release byte for byte. --no-mtp write a folder without the draft head (for serving with a separate head, or without drafting). config.json still says mtp_num_hidden_layers = 1; such a folder does not match the published list. --check compare DST, file by file, with SRC/SHA256SUMS.bf16 (or --sums FILE); exit status 1 on any difference. --check-only does only that, on an existing DST. Needs python >= 3.9 and torch (CPU). About 110 GB is read and 56 GB written; peak memory is about workers x 8 GB. """ import argparse import json import os import sys import torch sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) # the tools find each other in any python mode import fp32_common as c def convert_shard(job): shard, items, partial, exact = job tensors = {} for name, source in items: x = source.read(name) c.finite(name, x) if c.A_LOG.fullmatch(name): tensors[name] = ("F32", x) else: tensors[name] = ("BF16", c.to_bf16(x, exact)) digest, size = c.write_shard(os.path.join(partial, shard), tensors) return shard, digest, sum(c.tensor_bytes(kind, t) for kind, t in tensors.values()) def main(): parser = argparse.ArgumentParser(description=__doc__.split(c.NL)[0], allow_abbrev=False) parser.add_argument("masters") parser.add_argument("output") parser.add_argument("--mtp", metavar="FILE") parser.add_argument("--no-mtp", action="store_true") c.add_common_arguments(parser) args = parser.parse_args() sums = c.published_sums(args, os.path.join(args.masters, "SHA256SUMS.bf16")) if args.check_only: sys.exit(0 if c.check_sums(args.output, sums, args.workers) else 1) if args.no_mtp and sums: c.fail("a folder without the draft head cannot match the published list; drop --check or --no-mtp") torch.set_grad_enabled(False) torch.set_num_threads(args.threads) c.say("masters_to_bf16:", c.versions()) if not args.exact_rounding: c.self_test() folder, manifest = c.open_masters(args.masters) head = c.open_mtp(args.masters, manifest, args.mtp, args.no_mtp) plan, order, shard_of = c.export_plan(folder, manifest, head) out, partial = c.start_output(args.output) results = c.run_shards([(shard, items, partial, args.exact_rounding) for shard, items in plan], convert_shard, args.workers) digests = {shard: digest for shard, digest, _ in results} index = {"metadata": {"total_size": sum(size for _, _, size in results)}, "weight_map": {name: shard_of[name] for name in order}} with open(os.path.join(partial, c.INDEX), "w") as f: f.write(json.dumps(index, indent=2) + c.NL) c.copy_small_files(args.masters, manifest["small_files"], partial) c.finish(partial, out, digests, sums, args.workers) c.say("BF16 DONE %s: %d tensors in %d weight files, %.2f GB%s" % ( out, len(order), len(results), index["metadata"]["total_size"] / 1e9, "" if head is not None else "; NO DRAFT (MTP) HEAD in this folder: add one before serving with drafting")) if __name__ == "__main__": main()